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

Joining two tables created using DataObject


Go to End
Reply


13 Posts   3275 Views

Avatar
sca123

Community Member, 61 Posts

11 September 2010 at 4:37am

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

Avatar
UncleCheese

Forum Moderator, 4094 Posts

11 September 2010 at 5:12am

I think the 8th argument is a join clause.

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

Avatar
sca123

Community Member, 61 Posts

11 September 2010 at 6:23am

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

Avatar
UncleCheese

Forum Moderator, 4094 Posts

11 September 2010 at 6:28am

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

Avatar
guywatson

Community Member, 16 Posts

19 April 2011 at 7:45pm

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

Avatar
guywatson

Community Member, 16 Posts

2 May 2011 at 2:03pm

Please Uncle Cheese, any ideas

Thanks

Avatar
Carbon Crayon

Community Member, 598 Posts

3 May 2011 at 10:11am

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.

Avatar
guywatson

Community Member, 16 Posts

3 May 2011 at 11:22am

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

Go to Top