Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObjectManager Within DataObjectManager?

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 1702 Views
  • VictorH
    Avatar
    Community Member
    29 Posts

    DataObjectManager Within DataObjectManager? Link to this post

    Update: Go to page 2 and look at the first post for the final code to do this.

    I have created a ProfilePage.php page that has many Photo Sets.

    I also created a PhotoSet.php object that has a Title and many Photos but only managed to have the Title Field in the Pop up.

    I also created a Photo.php object that has a Caption and a Photo.

    What I'm having trouble is have the ability to add multiple Photos on the Photo Set pop-up of Profile Page.

    Is this even possible?

    Here is the code for all 3 pages.

    Again I want so that when I click on "Add Photo Set" on a Profile Page to also give me the ability to add multiple images to that Photo Set not just the Photo Set title.

    ProfilePage.php

    <?php

    class ProfilePage extends Page {
       
       static $has_many = array(
          'PhotoSets' => 'PhotoSet'
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $photosetfield = new DataObjectManager(
             $this,
             'PhotoSets',
             'PhotoSet',
             array(
                'PhotoSetTitle' => 'PhotoSetTitle'
             ),
             'getCMSFields_forPopup'
          );
          $photosetfield->setAddTitle('Photo Set');
          $fields->addFieldToTab('Root.Content.PhotoSets', $photosetfield);
          
          return $fields;
       }
    }

    class ProfilePage_Controller extends Page_Controller {}

    ?>

    PhotoSet.php

    <?php

    class PhotoSet extends DataObject {
       
       static $db = array(
          'PhotoSetTitle' => 'Varchar(255)'
       );
       
       static $has_one = array(
          'ProfilePage' => 'ProfilePage'
       );
       
       static $has_many = array(
          'Photos' => 'Photo'
       );
    }

    ?>

    Photo.php

    <?php

    class Photo extends DataObject {
       static $db = array(
          'PhotoCaption' => 'Varchar(255)'
       );
       
       static $has_one = array(
          'Attachment' => 'File'
       );
       
       public function getCMSFields_forPopup()   {
          return new FieldSet(
             new TextField('PhotoCaption'),
             new FileIFrameField('Attachment')
          );
       }

    }

    ?>

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Yup. Nested DataObjectManager has been out for several months now. It works just as you'd expect.. just put it in your getCMSFields function for the DataObject.

  • VictorH
    Avatar
    Community Member
    29 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Thanks for the reply UncleCheese.

    I'm guessing this has to be done to the PhotoSet.php and this is what I'm having trouble with. I don't know how to do this :\

    Can you help extend my version of the PhotoSet.php ?

    I tried and this is what I got which is obviously wrong and I have no idea how to fix.

    <?php

    class PhotoSet extends DataObject {
       
       static $db = array(
          'PhotoSetTitle' => 'Varchar(255)'
       );
       
       static $has_one = array(
          'ProfilePage' => 'ProfilePage'
       );
       
       static $has_many = array(
          'Photos' => 'Photo'
       );
       
       public function getCMSFields_forPopup()   {
          return new FieldSet(
             return new DataObjectManager(
                $this,
                'Photos',
                'Photo',
                array(
                   'PhotoCaption' => 'PhotoCaption'
                )
             );
          );
       }

    }

    ?>

  • VictorH
    Avatar
    Community Member
    29 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Holy moly I think I got it!

    Thanks Uncle Cheese.

    Here is my updated code for PhotoSet.php

    <?php

    class PhotoSet extends DataObject {
       
       static $db = array(
          'PhotoSetTitle' => 'Varchar(255)'
       );
       
       static $has_one = array(
          'ProfilePage' => 'ProfilePage'
       );
       
       static $has_many = array(
          'Photos' => 'Photo'
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $photofield = new FileDataObjectManager(
             $this,
             'Photos',
             'Photo',
             'Attachment',
             array(
                'PhotoCaption' => 'PhotoCaption'
             ),
             'getCMSFields_forPopup'
          );
          $photofield->setAddTitle('Photos');
          $fields->addFieldToTab('Root.Photos', $photofield);
          
          return $fields;
       }

    }

    ?>

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Cool!

  • VictorH
    Avatar
    Community Member
    29 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Alright I thought i had it

    I was able to get the FileDataObjectManager to appear on the pop-up but when I go to add a photo I get this warning.

    [Warning] Bad class value NULL passed to ClassInfo::ancestry()

    GET /localhost/admin/EditForm/field/PhotoSets/AddForm/field/Photos/upload?ctf[PhotoSets][start]=0&ctf[PhotoSets][per_page]=10&ctf[PhotoSets][showall]=0&ctf[PhotoSets][sort]=Created&ctf[PhotoSets][sort_dir]=DESC&ctf[PhotoSets][search]=&ctf[PhotoSets][filter]=

    Line 155 in /Users/victorhernandez/Sites/localhost/sapphire/core/ClassInfo.php

    }
    147    
    148    /**
    149     * @todo Improve documentation
    150     */
    151    static function ancestry($class, $onlyWithTables = false) {
    152       global $_ALL_CLASSES;
    153
    154       if(is_object($class)) $class = $class->class;
    155       else if(!is_string($class)) user_error("Bad class value " . var_export($class, true) . " passed to ClassInfo::ancestry()", E_USER_WARNING);
    156
    157       $items = $_ALL_CLASSES['parents'][$class];
    158       $items[$class] = $class;
    159       if($onlyWithTables) foreach($items as $item) {
    160          if(!DataObject::has_own_table($item)) unset($items[$item]);
    161       }

    Trace

    * Bad class value NULL passed to ClassInfo::ancestry()
    Line 155 of ClassInfo.php
    * ClassInfo::ancestry()
    Line 433 of ComplexTableField.php
    * ComplexTableField->getParentIdNameRelation(Photo,,has_one)
    Line 413 of ComplexTableField.php
    * ComplexTableField->getParentIdName(,Photo)
    Line 286 of FileDataObjectManager.php
    * FileDataObjectManager->UploadForm()
    Line 227 of FileDataObjectManager.php
    * FileDataObjectManager->upload(HTTPRequest)
    Line 129 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
    Line 143 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
    Line 143 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
    Line 143 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
    Line 143 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
    Line 119 of Controller.php
    * Controller->handleRequest(HTTPRequest)
    Line 277 of Director.php
    * Director::handleRequest(HTTPRequest,Session)
    Line 121 of Director.php
    * Director::direct(/admin/EditForm/field/PhotoSets/AddForm/field/Photos/upload)
    Line 118 of main.php

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    A couple tips -- in a DataObject subclass, you don't need to run parent::getCMSFields(). That's just for SiteTree objects. Just return a FieldSet. Also, you don't need to pass the 'getCMSFields_forPopup' to your constructor. Just name the function getCMSFields() and you should be fine without it.

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Photo needs a has_one PhotoSet

    1702 Views
Page: 1 2
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.