7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Getting Fields from related tables
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 971 Views |
-
Getting Fields from related tables

23 December 2009 at 11:35am Last edited: 23 December 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.
-
Re: Getting Fields from related tables

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'
-
Re: Getting Fields from related tables

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?
-
Re: Getting Fields from related tables

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; -
Re: Getting Fields from related tables

24 December 2009 at 5:25am
That's great - working perfectly now. Thanks for all your help.
| 971 Views | ||
|
Page:
1
|
Go to Top |

