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.

Data Model Questions

PageID always 0 for shild table


Reply

3 Posts   938 Views

Avatar
linkoovi

2 September 2010 at 12:56am (Last edited: 2 September 2010 12:56am), Community Member, 12 Posts

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

Avatar
swaiba

2 September 2010 at 1:15am Forum Moderator, 1796 Posts

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

Avatar
linkoovi

2 September 2010 at 1:23am Community Member, 12 Posts

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

Thanks mate!!!