Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 在CakePHP中删除前检查是否有相关项_Validation_Cakephp_Relationship - Fatal编程技术网

Validation 在CakePHP中删除前检查是否有相关项

Validation 在CakePHP中删除前检查是否有相关项,validation,cakephp,relationship,Validation,Cakephp,Relationship,我想检查一下,以确保在删除场馆之前,它没有任何活动 Venue hasMany Event Event belongsTo Venue 我相信我会在我的场馆模型中的beforeDelete功能中这样做-但是-除此之外,我不知道如何检查活动。。。我是否必须以某种方式包括对事件模型的访问?如果失败,是否有方法返回验证允许的特定错误消息?或我是在验证本身中这样做的吗?我是CakePHP新手,所以请对这个答案持保留态度 我相信,由于场地与活动有关系,您可以通过以下方式访问场地,而无需进行任何修改: $

我想检查一下,以确保在删除
场馆
之前,它没有任何
活动

Venue hasMany Event
Event belongsTo Venue

我相信我会在我的场馆模型中的
beforeDelete
功能中这样做-但是-除此之外,我不知道如何检查活动。。。我是否必须以某种方式包括对事件模型的访问?如果失败,是否有方法返回验证允许的特定错误消息?或我是在验证本身中这样做的吗?

我是CakePHP新手,所以请对这个答案持保留态度

我相信,由于场地与活动有关系,您可以通过以下方式访问场地,而无需进行任何修改:

$this->Event

来自场馆模型或控制器


然后,您应该能够使用查询查找该地点的任何活动。

这应该可以满足您的需要。它将在删除场馆之前检查活动计数,如果计数大于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);
}
使用此行为:

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

资料来源:


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

听起来不错,但是-你能提供更多的细节吗?谢谢,这似乎很有效。是否有一种方法可以设置错误消息而不仅仅是“场馆未被删除”?在控制器中,将删除内容包装在一个条件中,例如:如果(!$this->Venue->delete($id)){$this->Session->setFlash('Sorry,删除场馆时出现问题”);$this->redirect('action'=>'view',$id);}我想要一个特定的错误(如果可能的话)类似于验证错误。有没有办法通知他们,由于场馆有相关活动,所以删除没有发生?Ie-在验证区域中,我可以设置“message”=>“这就是它没有保存的原因”…等等,因为您没有调用验证方法。您正在对模型调用delete方法。在$this->vention->delete()调用之前调用beforeDelete()方法。因此,如果beforeDelete()返回false,$this->vention->delete()将返回false。在控制器中,检查delete是否返回true(已删除)或false(未删除)。设置会话闪存消息或使用$this->Set('errorMessage','yourrorror here');在您的视图中,回显$errorMessage以显示错误。但是,任何验证错误都不会使$this->vention-delete()返回false吗?那么-我怎么知道是什么导致它返回false呢?我想指定的是,它不会因为事件、验证错误或任何其他未知问题而被删除。(如果这是基本的,我很抱歉-非常感谢您的帮助)