Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

HasManyDataObjectManager breaks site


Reply

15 Posts   1667 Views

Avatar
MagicUK

26 July 2011 at 1:16am Community Member, 60 Posts

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.

Avatar
UncleCheese

26 July 2011 at 2:25am 4085 Posts

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.

Avatar
MagicUK

26 July 2011 at 3:01am Community Member, 60 Posts

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?

Avatar
UncleCheese

27 July 2011 at 1:18am 4085 Posts

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

Avatar
MagicUK

27 July 2011 at 9:24pm Community Member, 60 Posts

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',      
      
   );

Avatar
UncleCheese

28 July 2011 at 2:38am 4085 Posts

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;
   }

}

Avatar
MagicUK

28 July 2011 at 5:33am Community Member, 60 Posts

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