Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS3 custom joins

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

Page: 1
Go to End
Author Topic: 517 Views
  • EdP
    Avatar
    Community Member
    13 Posts

    SS3 custom joins Link to this post

    In SS2 I had:

    $conductors = DataObject::get('Performer', "PerformerType.IsConductor", "", "INNER JOIN `PerformerType` ON PerformerType.ID = SiteTree.ParentID"

    Could someone please give me a clue how to achieve the same thing using (I assume) innerJoin syntax?

    I have Performers and PerformerTypes tables (pages) - the latter has a flag (IsConductor). I am trying to extract just those Performers whose PerformerType is IsConductor, for a dropdown field on the back end.

    By the way I see backticks generate an error now.

    So far I have got:

    $conductors = Performer::get()->innerJoin("PerformerType", "PerformerType.ID = SiteTree.ParentID");
    $conductors->filter("IsConductor", true);

    but the filter is not being applied. There's probably a far more elegant way to do it anyway - having to refer to SiteTree doesn't feel right.

    Ed

  • UndefinedOffset
    Avatar
    Community Member
    27 Posts

    Re: SS3 custom joins Link to this post

    There's a few ways to do what your trying to fix what you have:

    $conductors = Performer::get()->innerJoin("PerformerType", "PerformerType.ID = SiteTree.ParentID");
    $conductors = $conductors->filter("IsConductor", true);

    Or you could do it all in one shot:

    $conductors = Performer::get()->innerJoin("PerformerType", "PerformerType.ID = SiteTree.ParentID")->filter("IsConductor", true);

    The reason your filter is not being applied is because each time you call a modifier on a DataList (which is what Performer::get() returns) it creates a clone before modifying. Which is why your filter doesn't get applied.

  • EdP
    Avatar
    Community Member
    13 Posts

    Re: SS3 custom joins Link to this post

    Many thanks - I think that was the one permutation I hadn't tried! And just in case anyone else stumbles here this:

    $conductors = $conductors->toDropdownMap('ID', 'Title', '(Select one)', true);

    becomes this:

          

    $conductorfield = new DropdownField('ConductorID', 'Conductor', $conductors->map('ID', 'Title'));
          $conductorfield->setEmptyString('(Select one)');
          $fields->addFieldToTab('Root.Main', $conductorfield, 'Content');

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