Check if it has a related item before deleting in CakePHP
I'd like to check to make sure that before a Venue
is deleted, it doesn't have any Events
tied t开发者_运维问答o it.
Venue hasMany Event
Event belongsTo Venue
I believe I'd do this in a beforeDelete
function in my Venue model - but - beyond that, I'm not sure how to check for Events... do I have to include access to the Event model somehow? If it fails, is there a way to return a specific error message like validation allows? Or... do I do this in the validation itself?
This should do what you need. It will check for a count of events before deleting a venue, then if the count is greater than 0 it will return false, preventing the deletion. Else, it will delete if there are no events associated.
// 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;
}
}
Or you can do this:
In your model add this method
function hasEvents($venue_id){
$count = $this->Event->find("count", array("conditions" => array("venue_id" => $venue_id)));
if ($count == 0) {
return false;
} else {
return true;
}
}
In your controller
if($this->Venue->hasEvents($venue_id)){
//display error message that you cannot delete because venue has events
} else {
$this->Venue->delete($venue_id);
}
use this behavior:
<?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;
}
}
?>
SOURCE: http://nik.chankov.net/2007/10/23/check-for-existing-childs-behaviour/
HTH.
I am new to CakePHP, so please take this answer with a grain of salt.
I believe since Venue has a relationship to Event you can access it without any modifications with something like:
$this->Event ...
from the Venue Model or Controller.
You should then me able to use a query to find any Event with that venue.
for use cakephp 3.5.1 try this..
in Products model
Products belongsTo Categories
in Categories controller
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;
}
精彩评论