Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » HasManyDataObjectManager breaks site

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: 1641 Views
  • MagicUK
    Avatar
    Community Member
    60 Posts

    Re: HasManyDataObjectManager breaks site Link to this post

    Really appreciate your reply UncleCheese.

    Basically I want the CMS user to be able to select some of the speakers in the dataobject. My client has a list of speakers but some of them will only be speaking at certain events. So I thought the HasMany allowed me to do this with the checkboxes? Maybe I'm misunderstanding something that is resulting in this mess?

    The thumbnail issue. I'm not sure at the minute if I want the client to do that. We have all the required images on our server (sized appropriately) so I wasn't worrying to much about that.

    Thanks UncleCheese. Look forward to your reply.

  • UncleCheese
    Avatar
    4085 Posts

    Re: HasManyDataObjectManager breaks site Link to this post

    If the speakers already exist, and you're just associating them with an event, why not just use checkboxes?

    In either case, I think you want a many_many relationship for that, not has_many.

  • MagicUK
    Avatar
    Community Member
    60 Posts

    Re: HasManyDataObjectManager breaks site Link to this post

    Thanks matey. I just added a checkbox and it is working for me as I needed! Why didn't I think of that. Would love the checkbox to be shown in the list view before the pop up but I assume thats not possible?

  • UncleCheese
    Avatar
    4085 Posts

    Re: HasManyDataObjectManager breaks site Link to this post

    I'm getting a little lost, but what you're describing sounds like the functionality of ManyManyDataObjectManager.

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • MagicUK
    Avatar
    Community Member
    60 Posts

    Re: HasManyDataObjectManager breaks site Link to this post

    Hey again UncleCheese. If I change the DataObect to a ManyMany using the following code I still get that 'Unknown field' error as per previous post.

    class SpeakersPage extends SiteTree {

       public static $db = array(
       'RightContent' => 'HTMLText',
       
       );

       public static $has_one = array(

       );
       
       public static $many_many = array(
          'MySpeakers' => 'Speakers',
          
       );
       
       
        function getCMSFields() {
    $fields = parent::getCMSFields();

    $tablefield = new ManyManyDataObjectManager(
    $this,
    'MySpeakers',
    'Speakers',
    array(
       'ShowSpeaker' => 'ShowSpeaker',
    'FirstName' => 'FirstName',
    'LastName' => 'FamilyName',
    'Position' => 'Position',
    'Company' => 'Company',
    'SpeakerImage' => 'SpeakerImage',
    ),
    'getCMSFields_forPopup'
    );

    $tablefield->setParentClass('Speakers');
    $fields->addFieldToTab( 'Root.Content.Speakers', $tablefield );

    my speakers class has the following code:

    public static $has_one = array(
          'Speaker' => 'SpeakersPage',
          'SpeakerImage' => 'Image',      
          
       );

  • UncleCheese
    Avatar
    4085 Posts

    Re: HasManyDataObjectManager breaks site Link to this post

    A few things...

    1) A many_many relation is reciprocated by $belongs_many_many, not $has_one.

    2) DataObject names should always be singular, because the class only controls one record at a time. "Speaker" instead of "Speakers."

    3) Use the conventional getCMSFields() in lieu of the _forPopup function. That way if you ever scale it to ModelAdmin or another LeftAndMain interface, you don't have to repeat yourself.

    4) Use $summary_fields for the same reason as above.

    5) I just pushed a patch for the field list bug into the Github repo.

    Here's your new code:

    <?php

    class SpeakersPage extends SiteTree {

    public static $db = array(
    'RightContent' => 'HTMLText',

    );

    public static $many_many = array(
    'Speakers' => 'Speaker',
    );

    function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab( 'Root.Content.Speakers', new ManyManyDataObjectManager(
             $this,
             'Speakers',
             'Speaker'
          ));
          return $fields;
       }

    }

    class SpeakersPage_Controller extends ContentController {}

    Speaker.php

    <?php

    class Speaker extends DataObject {

       static $db = array(
       'FirstName' => 'Text',
       'LastName' => 'Text',
       'Position' => 'Text',
       'Company' => 'Text',
       );
       
       
       public static $has_one = array(
        'SpeakerImage' => 'Image'
       );

       static $belongs_many_many = array (
          'SpeakerPages' => 'SpeakersPage'
       );

       static $summary_fields = array(
          'FirstName' => 'FirstName',
          'LastName' => 'FamilyName',
          'Position' => 'Position',
          'Company' => 'Company',
          'SpeakerImage' => 'SpeakerImage',
       );

       function getCMSFields() {
          $fields = new FieldSet();
          $fields->push( new TextField( 'FirstName', 'First Name' ) );
          $fields->push( new TextField( 'LastName' ) );
          $fields->push( new TextField( 'Position' ) );
          $fields->push( new TextField( 'Company' ) );
          $fields->push( new ImageField('SpeakerImage') );
          return $fields;
       }

    }

  • MagicUK
    Avatar
    Community Member
    60 Posts

    Re: HasManyDataObjectManager breaks site Link to this post

    Absolute legend mate. Thanks. That has really helped me.

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