我想检查一下,确保在删除Venue
之前,它没有绑定任何Events
。
Venue hasMany Event
Event belongsTo Venue
我相信我会在我的Venue模型中的beforeDelete
函数中执行此操作 - 但是 - 除此之外,我不确定如何检查事件...我是否必须包含对事件的访问权限不知怎的?如果它失败了,有没有办法返回像验证允许的特定错误消息?或者......我在验证本身中这样做吗?
答案 0 :(得分:2)
这应该做你需要的。它会在删除场地之前检查事件的数量,然后如果计数大于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);
}
答案 1 :(得分:0)
我是CakePHP的新手,所以请大家不要理解这个答案。
我相信,由于Venue与Event有关系,您无需修改即可访问它:
$ this->活动......
来自场地模型或控制器。
然后,您应该可以使用查询来查找具有该场地的任何事件。
答案 2 :(得分:0)
使用此行为:
<?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。
答案 3 :(得分:0)
使用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;
}