如何删除atk4中的子/子记录?

时间:2012-08-08 15:22:39

标签: php user-interface atk4

看起来模型钩子beforeDelete不能分层次地工作。 让我用例子来解释。

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');
    }
}

当我在“grand-parent”Model_User上调用delete()时,它会尝试按预期删除所有Item记录,但是从那里不执行Item.beforeDelete钩子并且在尝试删除之前不要删除Details记录项目

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我想我至少在层次结构模型结构中工作了。

这就是它的完成方式:

class Model_Object extends hierarchy\Model_Hierarchy {
    public $table = 'object';

    function init(){
        parent::init();
        $this->debug(); // for debugging
        $this->addField('name');
        $this->addHook('beforeDelete',$this);
    }

    function beforeDelete($m) {
        // === This is how you can throw exception if there is at least one child record ===
        // if($m->ref('Object')->count()->getOne()) throw $this->exception('Have child records!');

        // === This is how you can delete child records, but use this only if you're sure that there are no child/child records ===
        // $m->ref('Object')->deleteAll();

        // === This is how you can delete all child records including child/child records (hierarcialy) ===
        // Should use loop if we're not sure if there will be child/child records
        // We have to use newInstance, otherwise we travel away from $m and can't "get back to parent" when needed
        $c = $m->newInstance()->load($m->id)->ref('Object');
        // maybe $c = $m->newInstance()->loadBy('parent_id',$m->id); will work too?
        foreach($c as $junk){
            $c->delete();
        }
        return $this;
    }

}

这里重要的是:
*扩展层次结构\ Model_Hierarchy类而不是Model_Table
*在beforeDelete hook中使用适当的方法 如果我们有孩子,* *限制删除 - 抛出异常
* * deleteAll - 当您确定没有儿童/儿童记录时使用它 * * newInstance + loop + delete - 在必须删除记录(甚至是子/子/ ...)层次结构时使用它

也许有些人有更好的解决方案?