我無法找到關於如何在Silverstripe中正確保存has_one關係的線索。Silverstripe添加/保存對象到has_one關係
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();
但是,這段代碼什麼也沒做,沒有錯誤,但也沒有在數據庫中創建關係。
了我所能做的是:
$c->GarageID = $g->ID;
$c->write();
但這似乎不是很喜歡ORM ...
感謝,是的,這是正確的方式,儘管它看起來有點怪異「分配」車庫的汽車,當邏輯上這將是最好把車開進車庫;) – Asped