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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

stupid question


Go to End


4 Posts   1641 Views

Avatar
theAlien

Community Member, 131 Posts

6 September 2010 at 4:04am

Edited: 06/09/2010 4:05am

Hi,
I'm feeling really stupid...
I have 2 unrelated dataobjects (DO1 and DO2).
DO1 needs some data from DO2 to perform the right behaviour (everything on the model-side, so no templating).

What I have right now (in DO1) is:

function Address(){

    $a = DataObject::get('DO2', "ID=1");

    if($a){

        $c = $a->City;

        return $c;

    }

}

I've been checking too many times for typos so I suspect there's something else I'm doing wrong.
But what...???

Avatar
rob.s

Community Member, 78 Posts

6 September 2010 at 5:48am

Edited: 06/09/2010 5:54am

HI,

$a = DataObject::get('DO2', "ID=1"); 

... returns a DataObjectSet

i think you should use

$a = DataObject::get_by_id('DO2', 1); 

but please provide more information regarding the DataObjects (colums, relational definitions, etc.)

Avatar
theAlien

Community Member, 131 Posts

6 September 2010 at 6:28am

hi rob.s,
Thanks, that did the trick.
Always thought that all kinds of dataobject::get returned a dataobjectset (the one a little bit more filled than the other ;-) ).

Avatar
Martijn

Community Member, 271 Posts

6 September 2010 at 7:45am

DataObject::get_one('SomeDataObject', 'ID = ' . $someID), DataObject::get_by_id('SomeDataObject', $someID) returns the same single DataObject.

DataObject::get('SomeDataObject', $wherestatement); returns a DataObjectSet even if there is one DataObject that matches the $wherestatement. This means that you can use alle methods from DataObjectSet like First, Last, MoreThenOnePage, Count etc. in your php code or your template file (just the notation is little different).

Hope this explains it a little bit more.