Asped Asped - 1 month ago 3x
PHP Question

Silverstripe add/save object to has_one relation

I cannot find a clue on how to correctly save a has_one relation in Silverstripe.

class Car extends DataObject {
$has_one = array(

class Garage extends DataObject {
$has_many = array(
// let's say I have these records in the DB
$g = Garage::get()->ByID(111);
$c = Car::get()->ByID(222);

// I want to do sth like this to define the relation
$c->Garage = $g;

But this code does nothing, no error, but also the relation is not created in the DB.

What I could to is this:

$c->GarageID = $g->ID;

But this does not seem very ORM like...


there doesn't seem to be an extra method for adding has_one relations, but if you want to stick with the ORM, you could do it the other way around: