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

Getting Fields from related tables


Reply


5 Posts   1193 Views

Avatar
timcole

Community Member, 32 Posts

23 December 2009 at 11:35am

Edited: 23/12/2009 11:36am

This maybe a dumb question - I am still learning SilverStripe in general....

I am building a Song Library module for a client. My classes are:

Songs - Title, Lyrics, Author
Theme - Title
SubTheme - Title.

Songs have many SubThemes. SubThemes have one Theme.
I am using ModelAdmin and ManyManyDataObjectManager to provide a CMS interface for the library. MMDOM is used on the Songs CMS page to allow the user to select the SubThemes for the song. This is working great, but I am stuck with two things:

1. I want the MMDOM to display ALL the SubThemes not just the first 10 by default.
2. I want to include a column with the Theme for each SubTheme.

My current code for the cms is

function getCMSFields() {
$f = parent::getCMSFields();
$manager = new ManyManyDataObjectManager(
$this, // Controller
       'SubThemes', // Source name
'SubTheme', // Source class
array('Title'=>'Title'),
'getCMSFields_forPopup' );
$f->removeFieldFromTab('Root', 'SubThemes'); // replace the tab with MMDOM tab
$f->addFieldToTab('Root.Theme', $manager);
return $f; }

I can post my classes too if that helps. Any advice much appreciated.

Avatar
UncleCheese

Forum Moderator, 4096 Posts

23 December 2009 at 1:53pm

You can set the number of records per page using the setPerPage() function. You can also customize the indexes of the page lengths using setPerPageMap(array('100','200',300')), for instance, if 10,20,30,etc. is too small for you.

To show related fields, just use the dot syntax like you use on your templates.

SomeRelation.SomeField => 'Related object field'

Avatar
timcole

Community Member, 32 Posts

24 December 2009 at 12:37am

Thanks for this - but I'm sorry, I still can't get it to work. I have added the line:

array('Title'=>'Title','Theme.Title'=>'Theme'),

And that produces the following error in the CMS (via JavaScript):

Error: "Couldn't run query: SELECT `SubTheme`.*, `SubTheme`.ID, if(`SubTheme`.ClassName,`SubTheme`.ClassName,'SubTheme') AS RecordClassName, Theme.Title, IF(`SongID` IS NULL, '0', '1') AS Checked FROM `SubTheme` LEFT JOIN `Song_SubThemes` ON (`SubTheme`.`ID` = `SubThemeID` AND `SongID` = '2') GROUP BY `SubTheme`.ID ORDER BY Created DESC LIMIT 0, 10 Unknown column 'Theme.Title' in 'field list'" at line 401 of /Applications/MAMP/htdocs/sapphire/core/model/MySQLDatabase.php

Also, I can't get the SetPerPage() function working... can you give me an example line?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

24 December 2009 at 3:46am

In your SubTheme object, write a method like this:

public function getThemeName()
{
return $this->Theme()->Name;
}

(assuming the relation name is "Theme" and "Name" is a field of the theme object.. customise as needed)

then in your headings array,

'ThemeName' => 'Theme name'

I was wrong on setPerPage().. it's setPageSize()

$f->addFieldToTab($dom = new ManyManyDataObjectManager(..... ));
$dom->setPageSize(50);
return $f;

Avatar
timcole

Community Member, 32 Posts

24 December 2009 at 5:25am

That's great - working perfectly now. Thanks for all your help.