Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » stupid question

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

Page: 1
Go to End
Author Topic: 813 Views
  • theAlien
    Avatar
    Community Member
    131 Posts

    stupid question Link to this post

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

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: stupid question Link to this post

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

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: stupid question Link to this post

    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 ;-) ).

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: stupid question Link to this post

    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.

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