Jump to:

7939 Posts in 1472 Topics by 944 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: 1609 Views
  • VictorH
    Avatar
    Community Member
    29 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Big THANKS to you UncleCheese. After a little trial and error and guidance from you (tried my best not to ask you for more help) I finally figured it out.

    Here is the final code for the basics of what I've been trying to do. This currently works but if there is something messy that I should probably clean up please advise. I'm a designer not a developer and Silverstripe has been a great experience. I hope this helps others out there

    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'
             )
          );
          $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'
       );
       
       public function getCMSFields()   {
          return new FieldSet(
             new TextField('PhotoSetTitle'),
             new FileDataObjectManager(
                $this,
                'Photos',
                'Photo',
                'Attachment',
                array(
                   'PhotoCaption' => 'PhotoCaption'
                )
             )
          );
       }
    }

    ?>

    Photo.php

    <?php

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

    }

    ?>

    ProfilePage.ss

    $Content

    <% if PhotoSets %>
       <% control PhotoSets %>
          $PhotoSetTitle
          <% control Photos %>
             $Attachment
             $PhotoCaption
          <% end_control %>
       <% end_control %>
    <% end_if %>

    $Form
    $PageComments

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Looks good to me!

  • priithansen
    Avatar
    Community Member
    25 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Thank You good man this is exactly what I needed.

  • Muaddib
    Avatar
    Community Member
    4 Posts

    Re: DataObjectManager Within DataObjectManager? Link to this post

    Same situation, different problem:

    I have Project Pages, which can have assigned one Mentor, and multpile Participants as DataObjects.
    Each Mentor,Participant have multiple images.
    Almost similar to posts above, just i'm using HasOneDOM and HasManyDOM

    Geting error:

    [User Error] Couldn't run query: SELECT "Participant"."ClassName", "Participant"."Created", "Participant"."LastEdited", "Participant"."FullName", "Participant"."PictureID", "Participant"."ID", CASE WHEN "Participant"."ClassName" IS NOT NULL THEN "Participant"."ClassName" ELSE 'Participant' END AS "RecordClassName" FROM "Participant" WHERE ("" = '7') ORDER BY Created DESC Unknown column '' in 'where clause'

    It somehow doesn't make relations Participant=>something?

    ==========Project Page============

    class Project extends Page
    {
    static $has_one = array (
    'Mentor' => 'Mentor'
    );
    static $has_many = array (
    'Participants' => 'Participant'
    );

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

    $participants = new HasManyDataObjectManager(
    $this,
    'Participants',
    'Participant',
    array(
    'FullName'=>'FullName'
    )
    );

    $mentor = new HasOneDataObjectManager(
    $this,
    'Mentor',
    'Mentor',
    array(
    'FullName'=>'FullName'
    )
    );

    $fields->addFieldToTab('Root.Content.Mentor', $mentor);
    $fields->addFieldToTab('Root.Content.Participants', $participants);
    return $fields;
    }
    }


    ========== End Project Page============

    ==========Participant Page============

    class Participant extends DataObject
    {
    static $db = array (
    'FullName' => 'Text'
    );

    static $has_one = array(
    'Picture'=>'ImageResource'
    );

    static $has_many = array (
    'Projects' => 'Project',
    'Images' => 'ImageResource'
    );
    public function getCMSFields()
    {
    return new FieldSet(
    new TextField('FullName'),
    new SimpleImageField('Picture'),
    new FileDataObjectManager(
    $this,
    'Images',
    'ImageResource',
    'Attachment',
    array(
    'Description'=>'Description'
    )
    )
    );
    }
    }


    ==========End Participant Page============

    ==========Mentor Page============
    Same as Participant
    ==========End Mentor Page============

    ==========ImageResourcePage Page============

    class ImageResource extends DataObject {
    static $db = array(
    'Description'=>'Text'
    ;
    static $has_one = array(
    'Attachment'=>'Image',
    'Mentor'=>'Mentor',
    'Participant'=>'Participant'
    );
    public function getCMSFields() {
    return new FieldSet(
    new TextField('Description'),
    new FileIFrameField('Attachment')
    );
    }
    }

    ==========End ImageResourcePage Page============

    Totaly lost in space...

    1609 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.