I cannot find a clue on how to correctly save a has_one relation in Silverstripe.
class Car extends DataObject {
$has_one = array(
'garage'=>'Garage';
);
}
class Garage extends DataObject {
$has_many = array(
'cars'=>'Car';
);
}
// 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;
$c->write();
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;
$c->write();
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:
$g->cars()->add($c);