Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » PageID always 0 for shild table

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

Page: 1
Go to End
Author Topic: 908 Views
  • linkoovi
    Avatar
    Community Member
    12 Posts

    PageID always 0 for shild table Link to this post

    Hi,

    I have a table that will have PageID 0 (zero) it wont insert the ID of the User class <<uses>>

    class SomeData extends DataObject
    {
       public static $db = array(
          'Stuff' => 'Varchar(50)',
       );

       public static $has_one = array(
          "Page" => "ThaPage"
       );

    }

    and

    class ThaPage extends SiteTree{
    public static $has_many = array(
    "SomeDataz" => "SomeData" // ??? do I need this ???
    );

    }

    class ThaPage_Controller extends ContentController{

    public function doStuff(){
    $Stuff = new SomeData();
    $Stuff->Stuff = "some stuff to tbl";
    $Stuff->write();
    }
    }

    The PageID ( <- SiteTree.ID ) will be 0, Do I have to make a method for SomeData that will insert the PageID ?
    or im missing something?

    Thanks

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: PageID always 0 for shild table Link to this post

    Hi,

    Despite your friendly avatar - let's see if I can help...

    I don't think you *need* it, but to use the nice silverstripe functions for accessing relationships, yes it is useful

    "SomeDataz" => "SomeData" // ??? do I need this ???

    To set the page you could do...

    $Stuff = new SomeData();
    $Stuff->Stuff = "some stuff to tbl";
    $Stuff->setComponent ('Page', $this);
    $Stuff->write();

    or maybe...

    $Stuff = new SomeData();
    $Stuff->Stuff = "some stuff to tbl";
    $Stuff->PageID = $this->ID;
    $Stuff->write();

    ...but I am not sure about $this being page object, you might need to use $this->data( ) to get the record to set as the component or get the ID to set directly

  • linkoovi
    Avatar
    Community Member
    12 Posts

    Re: PageID always 0 for shild table Link to this post

    Ahhh.. the $Stuff->PageID = $this->ID worked!!! why to think simple when you can do it complicated :/

    Thanks mate!!!

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