在CakePHP中删除之前,请检查它是否具有相关项

| 我想检查一下,以确保在删除“ 0”之前,它没有任何“ 1”绑定。
Venue hasMany Event
Event belongsTo Venue
我相信我会在Venue模型的a3ѭ函数中执行此操作-但是-除此之外,我不确定如何检查事件...我是否必须以某种方式包括对事件模型的访问?如果失败,是否有办法返回特定的错误消息,如验证允许?还是...我是在验证本身中这样做的吗?     
已邀请:
        这应该做您需要的。它将在删除场所之前检查事件计数,然后如果计数大于0,它将返回false,从而阻止删除。否则,如果没有事件关联,它将删除。
// using app/models/venue.php
// In the following example, do not let a venue delete if it still contains events.
// A call of $this->Venue->delete($id) from VenueController.php has set $this->id .
// Assuming \'Venue hasMany Event\', we can access $this->Event in the model.
function beforeDelete(){
    $count = $this->Event->find(\"count\", array(\"conditions\" => array(\"venue_id\" => $this->id)));
    if ($count == 0) {
        return true;
    } else {
        return false;
    }
}
或者您可以这样做: 在您的模型中添加此方法
function hasEvents($venue_id){
    $count = $this->Event->find(\"count\", array(\"conditions\" => array(\"venue_id\" => $venue_id)));
    if ($count == 0) {
        return false;
    } else {
        return true;
    }
}
在您的控制器中
if($this->Venue->hasEvents($venue_id)){
    //display error message that you cannot delete because venue has events
} else {
    $this->Venue->delete($venue_id);
}
    
        我是CakePHP的新手,所以请带一点盐来回答这个问题。 我相信,由于Venue与Event有关系,因此您无需进行任何修改即可访问它,例如: $ this-> Event ... 来自场地模型或控制器。 然后,您应该可以使用查询查找该地点的任何活动。     
        使用此行为:
<?php
/**
 * Prevent deletion if child record found
 *
 * @author    Nik Chankov
 * @url    http://nik.chankov.net
 */
class HasChildrenBehavior extends ModelBehavior {
    /**
     * Empty Setup Function
    */
    function setup(&$model) {
        $this->model = $model;
    }

    /**
     * Run the check and cancel the deletion if child is found
     * @access public
     */
    function beforeDelete(){
        if(isset($this->model->hasMany)){
            foreach($this->model->hasMany as $key=>$value){
                $childRecords = $this->model->{$key}->find(\'count\', array(\'conditions\'=>array($value[\'foreignKey\']=>$this->model->id)));
                if($childRecords > 0){
                    return false;
                }
            }
        }
        //Checking habtm relation as well, thanks to Zoltan
        if(isset($this->model->hasAndBelongsToMany)){
            foreach($this->model->hasAndBelongsToMany as $key=>$value){
                $childRecords = $this->model->{$key}->find(\'count\', array(\'conditions\'=>array($value[\'foreignKey\']=>$this->model->id)));
                if($childRecords > 0){
                    return false;
                }
            }
        }
        return true;
    }
}
?>
消息来源:http://nik.chankov.net/2007/10/23/check-for-existing-childs-behaviour/ HTH。     
        使用cakephp 3.5.1试试这个.. 在产品模型中 产品属于类别 在类别控制器中
public function delete($id = null)
{
    $this->request->allowMethod([\'post\', \'delete\']);
    if($this->checkassociated($id) > 0){
        $this->Flash->error(__(\'this category could not be deleted.\'));
    }else{
        $category = $this->Categories->get($id);
        if ($this->Categories->delete($category)) {
            $this->Flash->success(__(\'this category has been deleted.\'));
        } else {
            $this->Flash->error(__(\'this category could not be deleted.\'));
        }
    }
    return $this->redirect([\'action\' => \'index\']);
}

public function checkassociated($category_id){
    $itemsTable = TableRegistry::get(\'Products\');
    $itemdata = $itemsTable->find(\'all\')
        ->where([\'Products.category_id\'=>$category_id]);
    $number = $itemdata->count();
    return $number;
}
    

要回复问题请先登录注册