Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Getting Fields from related tables

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1067 Views
  • timcole
    Avatar
    Community Member
    32 Posts

    Getting Fields from related tables Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Getting Fields from related tables Link to this post

    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'

  • timcole
    Avatar
    Community Member
    32 Posts

    Re: Getting Fields from related tables Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Getting Fields from related tables Link to this post

    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;

  • timcole
    Avatar
    Community Member
    32 Posts

    Re: Getting Fields from related tables Link to this post

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

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