drl37530 2012-08-08 15:22
浏览 32
已采纳

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

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 ?

  • 写回答

1条回答 默认 最新

  • duanliang789262 2012-08-09 10:40
    关注

    I guess I got it working at least for hierarchial Model structure.

    This is how it was done:

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

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持