It looks that Model hook beforeDelete don’t work hierarchialy.
Let me explain with example.
class Model_User extends Model_Table{
public $table='user';
function init(){
parent::init();
$this->debug();
$this->addField('name');
$this->hasMany('Item');
$this->addHook('beforeDelete',$this);
}
function beforeDelete($m){
$m->ref('Item')->deleteAll();
}
}
class Model_Item extends Model_Table{
public $table='item';
function init(){
parent::init();
$this->debug();
$this->addField('name');
$this->hasOne('User');
$this->hasMany('Details');
$this->addHook('beforeDelete',$this);
}
function beforeDelete($m){
$m->ref('Details')->deleteAll();
}
}
class Model_Details extends Model_Table{
public $table='details';
function init(){
parent::init();
$this->debug();
$this->addField('name');
$this->hasOne('Item');
}
}
When I call delete() on “grand-parent” Model_User, then it tries to delete all Item records as intended, but from there don’t execute Item.beforeDelete hook and don’t delete Details records before trying to delete Item.
What I’m doing wrong ?
I guess I got it working at least for hierarchial Model structure.
This is how it was done:
Important things here are:
* extend hierarchy\Model_Hierarchy class not Model_Table
* use appropriate method in beforeDelete hook
* * restrict delete if we have children – throw exception
* * deleteAll – use it when you’re sure that there will be no child/child records
* * newInstance + loop + delete – use it when you have to delete records (even child/child/…) hierarchialy
Maybe some of you have a better solution?