Jump to:

7937 Posts in 1537 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » [solved] Multiple files on a dataobjekt (Model Admin)

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 562 Views
  • Pipifix
    Avatar
    Community Member
    54 Posts

    [solved] Multiple files on a dataobjekt (Model Admin) Link to this post

    Hello everybody.

    i'm german and i hope i can explain my problem in a understandable way. i've got a bunch of pdfs and docs (organized by kickassets) that i wanna conncect to a dataobject called 'Projekt' . Some of the files are unique to the projects (=Projekt). Some files belong to almost all projects. Examples for the files could be ‘description-project1.pdf‘ or ‘project-application-form.doc‘. I've tried a solution according to this code http://www.silverstripe.org/dataobjectmanager-module-forum/show/15656#post298390

    At first i thought i need a description for the file so i set a descritption. But i realized any file got a name. Maybe i can use this name instead the descrition for a better explanation of the downloadable file (user centric view).

    Here is my code:
    ResourceFile.php

    <?php
    class ResourceFile extends File {

    static $has_one = array (
    'Resource' => 'Resource'
    );
    }

    Resource.php:

    <?php
    class Resource extends DataObject
    {
    static $db = array (
    'Name' => 'Text',
    'Description' => 'Text'
    );

    static $has_many = array (
    'Attachments' => 'ResourceFile'
    );

    static $has_one = array (
    'Projekt' => 'Projekt'
    );

    public function getCMSFields_forPopup()
    {
    return new FieldSet(
       new TextField('Name'),
       new TextareaField('Description'),
       new MultipleFileUploadField('Attachments')
       );
    }
    }


    Maybe the description is obsolet. Is the has_one relation right? because one file can be attached to many projects so i guess i have to use has_many. but there i a strange error.

    At least the Projekt-Dataobject

    Projekt.php

    <?php

    class Projekt extends DataObject
    {
       static $db = array(
          'Title' => 'Varchar(255)',
          'Description' => 'HTMLText',
          'Long' => 'Int',
          'Lat' => 'Int',
          'URLSegment' => 'Varchar(255)',
          'ProjectCat' => "Enum('Soziales, Strasse, Wirtschaft')",
          'ProjectStatus' => "Enum('Planung, Durchfuehrung, Abgeschlossen')",
          'StatusText' => 'Varchar(255)',
          'MetaTitle' => 'Varchar(255)'
       );

       //Set our defaults
       static $defaults = array(   
          'Title' => 'Neues Projekt',
          'URLSegment' => 'neues-projekt'
       );
       
       static $has_one = array(
          'Image' => 'Image',
          'LinkedPage' => 'SiteTree'
       );
       
       static $has_many = array (
       'Resources' => 'Resource'
       );
       
       
       //Relate to the category pages
       static $belongs_many_many = array(
          'Categories' => 'CategoryPage'
       );
       
       //Fields to show in ModelAdmin table
       static $summary_fields = array(
          'Title' => 'Title',
          'URLSegment' => 'URLSegment',
          'ProjectStatus' => 'ProjectStatus',
          'ProjectCat' => 'ProjectCat',
       );   

       //Add an SQL index for the URLSegment
       static $indexes = array(
          "URLSegment" => true
       );   

       //Fields to search in ModelAdmin
       static $searchable_fields = array (
          'Title',
          'ProjectStatus',
          'ProjectCat',
          'Categories.ID' => array(
             'title' => 'Category'
          )
       );

       function getCMSFields()
       {
          $fields = parent::getCMSFields();

          //Main Tab
          $fields->addFieldToTab("Root.Main", new TextField('Title', 'Titel/Name des Projektes'));   
          $fields->addFieldToTab("Root.Main", new TextField('MetaTitle', 'Untertitle zum Projekt'));   
          .
          .
          .

          //DownloadResourcen      
          $manager = new DataObjectManager(
          $this, // Controller
          'Resources', // Source name
          'Resource', // Source class
          array(
          'Name' => 'Name',
          'Description' => 'Description',
          'Attachment.Name' => 'Attachment',
          ), // Headings
          'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
          // Filter clause
          // Sort clause
          // Join clause
          );

          // If undefined, all types are allowed. Pass with or without a leading "."
          //$manager->setAllowedFileTypes(array('pdf','doc'));
          
          // Label for the upload button in the popup
          //$manager->setBrowseButtonText("Upload (PDF or DOC only)");
          
          // In grid view, what field will appear underneath the icon. If left out, it defaults to the file title.
          //$manager->setGridLabelField('Name');
          
          // Plural form of the objects being managed. Used on the "Add" button.
          // If left out, this defaults to [MyObjectName]s
          //$manager->setPluralTitle('Resources');
          
          $fields->addFieldToTab("Root.Dateien", $manager);
          
          //$fields->addFieldToTab("Root.Dateien", new TextField('DownloadItem', 'Download-Datei'));
          
          return $fields;
       }

       //some functions
       
    }

    The resource tab on a project in modelladmin is shown. The popup is working and saving a file works well. But the saved ressource is not shown in the modelladminlisting.

    EDIT: In the popup the upload functionality works. i can import an existing file and i see (so far) this file in the attached files block. When I save his popup a green message appears ('Added new Resource successfully') but the file in the attached files block disappears.
    EDIT_END:

    Plus: If i comment out the setAllowedFileTypes or the setBrowseButtonText there is an error.?
    Whats wrong with that code? Is this the wrong way to attach multiple files?

    Thanks for your help.
    Pipifix

  • Pipifix
    Avatar
    Community Member
    54 Posts

    Re: [solved] Multiple files on a dataobjekt (Model Admin) Link to this post

    Hello again.

    I kept searching and found this useful snippet at the docs:
    http://doc.silverstripe.org/old/modules:dataobjectmanager#filedataobjectmanager

    This solved the problem and the objective (attaching files on dataobjects) is achieved.

    Pipifix

    562 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.