Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Joining two tables created using DataObject

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 3120 Views
  • sca123
    Avatar
    Community Member
    61 Posts

    Joining two tables created using DataObject Link to this post

    In page.php, I have a dataObject called "Specification" which has a dropdown field which pulls in the records from another dataobject called "yachtType". The relationship between the two dataobjects (tables) is yachtType.ID = Specification.yachtTypeID

    As part of the Specification table output in the CMS, I wish to show fields from both dataobjects (tables).

    $manager = new DataObjectManager(
    $this,
    'Specifications',
    'Specification',
    array('Model' => 'Model','Type' => 'Type'),
    'getCMSFields_forPopup'
    );

    Where "Model" is from the Specification table, and "Type" is from the yachtType table.

    However, "Type" returns a blank field in the table. "Model" displays correctly as does "yachtTypeID" if I use this.

    How do I show a value from the "yachtType" table?

    Thank you in advance

  • UncleCheese
    Avatar
    4085 Posts

    Re: Joining two tables created using DataObject Link to this post

    I think the 8th argument is a join clause.

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • sca123
    Avatar
    Community Member
    61 Posts

    Re: Joining two tables created using DataObject Link to this post

    Sorry UC, not really sure what you are saying, could you give an example?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Joining two tables created using DataObject Link to this post

    As the last argument of the DOM constructor, you can place a custom join clause.. Look at the construct function:

       function __construct($controller, $name = null, $sourceClass = null, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = null, $sourceJoin = "")

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • guywatson
    Avatar
    Community Member
    15 Posts

    Re: Joining two tables created using DataObject Link to this post

    Hi UncleCheese

    I understand i need to add an eighth argument in the constructor, however it is still not working.
    This is what i have

    $sourceJoin = "LEFT JOIN LockRange ON Lock.MyLockRangeID=LockRange.ID";

    $lockDom = new ManyManyDataObjectManager(
    $this,
    'MyLocks',
    'Lock',
    array(
    'Title' => 'Title',
       'MyLockRangeID' => 'RangeID',
       'RangeTitle' => 'RangeTitle',
       'LockRange.ID' => 'LockRangeID'
    ),
    'getCMSFields_forPopup',
    "",
    "",
    $sourceJoin
    );

    and i get the error

    [User Error] Uncaught Exception: Object->__call(): the method 'lockrange' does not exist on 'Lock'

    Thankyou for any help

    Guy

  • guywatson
    Avatar
    Community Member
    15 Posts

    Re: Joining two tables created using DataObject Link to this post

    Please Uncle Cheese, any ideas

    Thanks

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Joining two tables created using DataObject Link to this post

    Hi guys,

    There is actually a much easier way of doing this:

    1st make your life a litte easier and reduce your DOM definition to the following:

    $lockDom = new ManyManyDataObjectManager(
    $this,
    'MyLocks',
    'Lock'
    );

    Now in Lock.php first change getCMSFields_forPopup() to just getCMSFields(), DOM will now automatically pick this up so you don't need to add it to the definition. Also in this class add the following:

    static $summary_fields = array(
    'Title' => 'Title',
    'MyLockRange.ID' => 'RangeID',
    'MyLockRange.RangeTitle' => 'RangeTitle'
    );

    I am assuming your has_one relationship is called MyLockRange and that it has a Title field along with it's ID.

    Anyway, I hope that helps

    Aram

    www.ssbits.com - Your one stop SilverStripe learning resource.

  • guywatson
    Avatar
    Community Member
    15 Posts

    Re: Joining two tables created using DataObject Link to this post

    Hi Aram

    Sorry i didnt expain myself correctly. I am trying to do this inside Hardware.php. I have a manymany relationship. I need to bring up a list of Locks inside a Hardware tab.

    So my problem is that in the Lock database i only have the RangeID. I want to look this ID up and put the rangeTitle on the list instead of the range ID.

    Thanks

    Guy

    3120 Views
Page: 1 2
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.