删除父表中的记录时如何在子表中设置parent_id=NULL?
这就像 MySQL INNODB 表中的 ON DELETE = SET NULL 一样,但我想避免在 INNODB 级别使用所有这些(级联、忽略、更新和设置 null)功能,并将其移至 atk4 模型以将所有这些逻辑保留在一个地方。
例如,
class Model_Parent extends Model_Table{
public $table='parent';
function init(){
parent::init();
$this->addField('name');
$this->hasMany('Child');
$this->addHook('beforeDelete',$this);
}
function beforeDelete($m){
// I guess here I should somehow set parent_id=NULL in all related Model_Child
// records, but when I do so, then it's again DB constraint violation of course
$c = $m->ref('Child');
foreach($c as $junk){
$c->set('parent_id',NULL); // this and below is not working
$c->save();
}
}
}
class Model_Child extends Model_Table{
public $table='child';
function init(){
parent::init();
$this->addField('name');
$this->hasOne('Parent');
}
}