Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Portfolio Page with $has_one AND $has_many

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

Page: 1
Go to End
Author Topic: 2810 Views
  • pklopp
    Avatar
    Community Member
    4 Posts

    Portfolio Page with $has_one AND $has_many Link to this post

    Maybe someone can help me:

    I need to create a portfoliopage. The portfolio Page has a $has_many(Projects). Every Project should have 1 to many images.

    But when I create ComplexTableField for Projects, everything is good, but then I need to attach the images with another ComplexTableField inside the Popup-Window and it doesnt show me buttons to add or edit the files.

    Is there any better way to do it?

    Regards,

    PKLOPP

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Portfolio Page with $has_one AND $has_many Link to this post

    I don't believe Complex Table Fields inside other CTFs work very well, if at all. I guess one way you could get this to work is use model admin to manage projects. Then in the edit interface it will have a separate tab for the managing images. Then to attach projects to a page you could use a HasManyComplexTableField. But still you would need to use model admin to manage images.

    Another option would be to have 2 CTFs on your page. One for projects and one for images. The images tab would filter the list (see docs for adding filters) so that it only showed the images for projects which are attached to the same page.

  • pklopp
    Avatar
    Community Member
    4 Posts

    Re: Portfolio Page with $has_one AND $has_many Link to this post

    thanks a lot!

    At the Moment I'm thinking about making every portfoliopage to a "real" page and then adding there with $has_many images...

  • patjnr
    Avatar
    Community Member
    101 Posts

    Re: Portfolio Page with $has_one AND $has_many Link to this post

    Hi

    try this

    set you has one relationship in ProjectPage.php

    class ProjectPage extends Page {
       
       static $has_one = array(
          'PortfolioPage' => 'PortfolioPage'
       );
    ..............

    PortfolioPage.php

       static $has_many = array (

          'Projects' => 'ProjectPage ',
       );
       public function getCMSFields()   {
          $f = parent::getCMSFields();
          
          $tablefield = new HasManyComplexTableField(
           $this,
           'Projects',
           'ProjectPage ',
           array(
             'Title' => 'Title',
           ),
           'getCMSFields_forPopup'
          );
          $tablefield->setPermissions(array("show"));
          
          $f->addFieldToTab( 'Root.Content.Projects', $tablefield );
       
          return $f;
       }
       
       public function onBeforeWrite()
       {
          $beforePage = DataObject::get("ProjectPage ", 'PortfolioPage="1"' ');
          if($beforePage)
          {
             foreach($beforePage as $page) {
                $page->writeToStage('Stage');
                $page->publish('Stage', 'Live');
                $page->flushCache();
             }
          }
          parent::onBeforeWrite();
       }

    tutorial here http://doc.silverstripe.org/doku.php?id=hasmanycomplextablefield

    now your structure could be like this.

    ProjectHolder
    ProjectPage(1)
    ProjectPage(2)
    ProjectPage(3)
    ProjectPage(x)
    ...

    PortfolioHolder
    PortfolioPage

    in portfolio page you will have a HasManyComplexTableField showing all your project which are of page Type ProjectPage
    In an portfolio just tick the projects you want to associate with that Portfolio you are in.
    as for the image in you Project page under $has_many put this line

       static $has_many = array (
          'Images' => 'Resource',
       );
       protected static $_cached_pages = null;
       
       public function getCMSFields()   {
          $f = parent::getCMSFields();
          $fileMan = new ImageDataObjectManager(
          $this,
             'Images',
             'Resource',
             'Attachment',
          array(
          'Name' => 'Name',
          'Caption' => 'Caption',
          ),
          'getCMSFields_forPopup'
          );
          $fileMan->setAllowedFileTypes(array('jpg'));
          $fileMan->setGridLabelField('Name');
          $fileMan->setAddTitle('Images');
          $f->addFieldToTab("Root.Content.Images", new HeaderField(
             $title = "Images",
             $headingLevel = "2"
          ));
          $f->addFieldToTab("Root.Content.Images", $fileMan);
          
       
          
       
           return $f;
       }

    Resource.php

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

       static $has_one = array (
          'Attachment' => 'Image',
          'ProjectPage ' => 'ProjectPage ',
       );

       public function getCMSFields_forPopup()
       {
          return new FieldSet(
          new TextField('Name'),
          new TextareaField('Caption'),
          new FileIFrameField('Attachment' , 'Image .. (Please don&acute;t upload an image which is more than 1mb)')
          );
       }

    }
    ?>

    for the images tutorials here
    http://doc.silverstripe.org/doku.php?id=modules:dataobjectmanager&s=resource%20php

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