Re: Behavior method won't work
Okay, I think I got it. I've made a single function as you suggested. *Controller* public function delete($id) > { > if ($this->request->is('get')) { > throw new MethodNotAllowedException(); > } > > $this->Attraction->id = $id; > > if ($this->Attraction->softDelete($id)) { > $this->Session->setFlash(__('The attraction with id %s was > successfully deleted.', h($id)), 'default', array( > 'class' => 'alert alert-success' > )); > } > > return $this->redirect(array( > 'action' => 'index' > )); > } *AppModel* class AppModel extends Model { > > // Soft delete functionaliteit > > public function beforeDelete() > { > if ($this->hasField('deleted')) { > $this->saveField('deleted', date('Y-m-d H:i:s')); > } > > return false; > } > > public function softDelete($id) > { > $this->delete($id); > > $record = $this->find('count', array( > 'conditions' => array( > "{$this->alias}.id" => $id, > "{$this->alias}.deleted !=" => null > ) > )); > if ($record > 0) { > return true; > } > } > } And that works fine for me now! Stephen, thank you for you time and thank you very much to support me on this one! I've pasted my code here so others can use this functionality too! ;) -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Apologies, my example should say if(! $this->Attraction->isDeleted($id)) to match the flash message. On 27 July 2014 11:36, Stephen S wrote: > You can create a custom app model method to overcome this, something like > this: > > public function isDeleted($recordId) { > if(! $this->exists($recordId)) { > return true; > } > > if(! $this->hasField('deleted')) { > return false; > } > > $record = $this->find('count', array( > 'conditions' => array( > "{$this->alias}.id" => $recordId, > 'NOT' => array("{$this->alias}.deleted' => null) > ) > ); > > return ($record > 0) ? true : false; > } > > Then within your controller, something like this: > > $this->Attraction->delete($id); > if($this->Attraction->isDeleted($id)) { > $this->Session->setFlash(__('Attraction could not be deleted')); > } > > All the model method will do is check if the row doesn't exist (true, hard > delete), if it does exist and doesn't have the deleted field then soft > delete isn't possible so false it isn't deleted, otherwise count if the > record exists with deleted not set to null and return true or false based > on the result. > > This is the main issue with using $this->Model->delete() for both soft and > hard delete, if you created your own method like > $this->Model->detectAndDelete() which deletes using the appropriate method > by checking hasField('deleted') you wouldn't need the new model check. > > Anyhow I hope this helps. > > > On 27 July 2014 11:22, Sam Clauw wrote: > >> Allright, I should know that, you're totally right! It works for my now, >> except my flash error of course. I could put it outside the if >> ($this->Attraction->delete($id)) {} check, but then the flash would always >> been shown, even if there was no delete... >> >> -- >> Like Us on FaceBook https://www.facebook.com/CakePHP >> Find us on Twitter http://twitter.com/CakePHP >> >> --- >> You received this message because you are subscribed to the Google Groups >> "CakePHP" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to cake-php+unsubscr...@googlegroups.com. >> To post to this group, send email to cake-php@googlegroups.com. >> Visit this group at http://groups.google.com/group/cake-php. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Kind Regards > Stephen Speakman > -- Kind Regards Stephen Speakman -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
You can create a custom app model method to overcome this, something like this: public function isDeleted($recordId) { if(! $this->exists($recordId)) { return true; } if(! $this->hasField('deleted')) { return false; } $record = $this->find('count', array( 'conditions' => array( "{$this->alias}.id" => $recordId, 'NOT' => array("{$this->alias}.deleted' => null) ) ); return ($record > 0) ? true : false; } Then within your controller, something like this: $this->Attraction->delete($id); if($this->Attraction->isDeleted($id)) { $this->Session->setFlash(__('Attraction could not be deleted')); } All the model method will do is check if the row doesn't exist (true, hard delete), if it does exist and doesn't have the deleted field then soft delete isn't possible so false it isn't deleted, otherwise count if the record exists with deleted not set to null and return true or false based on the result. This is the main issue with using $this->Model->delete() for both soft and hard delete, if you created your own method like $this->Model->detectAndDelete() which deletes using the appropriate method by checking hasField('deleted') you wouldn't need the new model check. Anyhow I hope this helps. On 27 July 2014 11:22, Sam Clauw wrote: > Allright, I should know that, you're totally right! It works for my now, > except my flash error of course. I could put it outside the if > ($this->Attraction->delete($id)) {} check, but then the flash would always > been shown, even if there was no delete... > > -- > Like Us on FaceBook https://www.facebook.com/CakePHP > Find us on Twitter http://twitter.com/CakePHP > > --- > You received this message because you are subscribed to the Google Groups > "CakePHP" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to cake-php+unsubscr...@googlegroups.com. > To post to this group, send email to cake-php@googlegroups.com. > Visit this group at http://groups.google.com/group/cake-php. > For more options, visit https://groups.google.com/d/optout. > -- Kind Regards Stephen Speakman -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Allright, I should know that, you're totally right! It works for my now, except my flash error of course. I could put it outside the if ($this->Attraction->delete($id)) {} check, but then the flash would always been shown, even if there was no delete... -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Your redirect is within the block "if($this->Attraction->delete($id))" but if you recall you return false in beforeDelete to cancel the operation meaning the controller thinks the delete has failed. Try this: public function delete($id) { if ($this->request->is('get')) { throw new MethodNotAllowedException(); } $this->Attraction->id = $id; if ($this->Attraction->delete($id)) { $this->Session->setFlash(__('De attractie met id %s werd succesvol verwijderd.', h($id)), 'default', array( 'class' => 'alert alert-success' )); } return $this->redirect(array( 'action' => 'index' )); } On 27 July 2014 10:24, Sam Clauw wrote: > Well, the weird thing is that's how I originally wrote it, and that won't > work. An illustration of how it all looks right now: > > *Controller (AttractionsController.php)* > > public function delete($id) > { > if ($this->request->is('get')) { > throw new MethodNotAllowedException(); > } > > $this->Attraction->id = $id; > > if ($this->Attraction->delete($id)) { > $this->Session->setFlash(__('De attractie met id %s werd succesvol > verwijderd.', h($id)), 'default', array( > 'class' => 'alert alert-success' > )); > > return $this->redirect(array( > 'action' => 'index' > )); > } > } > > *AppModel* > > class AppModel extends Model { > > // soft delete functionality > > public function beforeDelete() > { > if ($this->hasField('deleted')) { > if ($this->saveField('deleted', date('Y-m-d H:i:s'))) { > return false; > } > } > } > } > > I'm still getting the error: > > Missing View >> >> *Error: *The view for *AttractionsController::**delete()* was not found. >> *Error: *Confirm you have created the file: >> D:\Websites\BellewaerdeFun\app\Plugin\CoasterCms\View\Attractions\delete.ctp > > -- > Like Us on FaceBook https://www.facebook.com/CakePHP > Find us on Twitter http://twitter.com/CakePHP > > --- > You received this message because you are subscribed to the Google Groups > "CakePHP" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to cake-php+unsubscr...@googlegroups.com. > To post to this group, send email to cake-php@googlegroups.com. > Visit this group at http://groups.google.com/group/cake-php. > For more options, visit https://groups.google.com/d/optout. > -- Kind Regards Stephen Speakman -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Well, the weird thing is that's how I originally wrote it, and that won't work. An illustration of how it all looks right now: *Controller (AttractionsController.php)* public function delete($id) { if ($this->request->is('get')) { throw new MethodNotAllowedException(); } $this->Attraction->id = $id; if ($this->Attraction->delete($id)) { $this->Session->setFlash(__('De attractie met id %s werd succesvol verwijderd.', h($id)), 'default', array( 'class' => 'alert alert-success' )); return $this->redirect(array( 'action' => 'index' )); } } *AppModel* class AppModel extends Model { // soft delete functionality public function beforeDelete() { if ($this->hasField('deleted')) { if ($this->saveField('deleted', date('Y-m-d H:i:s'))) { return false; } } } } I'm still getting the error: Missing View > *Error: *The view for *AttractionsController::**delete()* was not found. > *Error: *Confirm you have created the file: > D:\Websites\BellewaerdeFun\app\Plugin\CoasterCms\View\Attractions\delete.ctp -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Also all that $this->referer() does is redirect the user back to the page they used previously if applicable, you can also pass an array such as return $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home')); On 27 July 2014 10:08, Stephen S wrote: > Yeah the redirect should be done from within the controller, models are > more for dealing with data / queries. Placing this in your controller > method should fix your issue, i.e. your admin_delete or delete method in > the controller. > > > On 27 July 2014 09:44, Sam Clauw wrote: > >> Hmmm, that doesn't work out for me. It seems that it tries to execute a >> query: >> >> *Error: *SQLSTATE[42000]: Syntax error or access violation: 1064 You >>> have an error in your SQL syntax; check the manual that corresponds to your >>> MySQL server version for the right syntax to use near 'referer' at line 1 >>> *SQL Query: *referer >> >> >> This is my current AppModel: >> >> class AppModel extends Model { >>> >>> // soft delete functionaliteit >>> >>> public function beforeDelete() >>> { >>> if ($this->hasField('deleted')) { >>> if ($this->saveField('deleted', date('Y-m-d H:i:s'))) { >>> return $this->redirect($this->referer()); >>> } >>> } >>> } >>> } >> >> >> I guess $this->redirect() should always been done in a controller? Or is >> there a way to catch it in the model? >> >> -- >> Like Us on FaceBook https://www.facebook.com/CakePHP >> Find us on Twitter http://twitter.com/CakePHP >> >> --- >> You received this message because you are subscribed to the Google Groups >> "CakePHP" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to cake-php+unsubscr...@googlegroups.com. >> To post to this group, send email to cake-php@googlegroups.com. >> Visit this group at http://groups.google.com/group/cake-php. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Kind Regards > Stephen Speakman > -- Kind Regards Stephen Speakman -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Yeah the redirect should be done from within the controller, models are more for dealing with data / queries. Placing this in your controller method should fix your issue, i.e. your admin_delete or delete method in the controller. On 27 July 2014 09:44, Sam Clauw wrote: > Hmmm, that doesn't work out for me. It seems that it tries to execute a > query: > > *Error: *SQLSTATE[42000]: Syntax error or access violation: 1064 You have >> an error in your SQL syntax; check the manual that corresponds to your >> MySQL server version for the right syntax to use near 'referer' at line 1 >> *SQL Query: *referer > > > This is my current AppModel: > > class AppModel extends Model { >> >> // soft delete functionaliteit >> >> public function beforeDelete() >> { >> if ($this->hasField('deleted')) { >> if ($this->saveField('deleted', date('Y-m-d H:i:s'))) { >> return $this->redirect($this->referer()); >> } >> } >> } >> } > > > I guess $this->redirect() should always been done in a controller? Or is > there a way to catch it in the model? > > -- > Like Us on FaceBook https://www.facebook.com/CakePHP > Find us on Twitter http://twitter.com/CakePHP > > --- > You received this message because you are subscribed to the Google Groups > "CakePHP" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to cake-php+unsubscr...@googlegroups.com. > To post to this group, send email to cake-php@googlegroups.com. > Visit this group at http://groups.google.com/group/cake-php. > For more options, visit https://groups.google.com/d/optout. > -- Kind Regards Stephen Speakman -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Hmmm, that doesn't work out for me. It seems that it tries to execute a query: *Error: *SQLSTATE[42000]: Syntax error or access violation: 1064 You have > an error in your SQL syntax; check the manual that corresponds to your > MySQL server version for the right syntax to use near 'referer' at line 1 > *SQL Query: *referer This is my current AppModel: class AppModel extends Model { > > // soft delete functionaliteit > > public function beforeDelete() > { > if ($this->hasField('deleted')) { > if ($this->saveField('deleted', date('Y-m-d H:i:s'))) { > return $this->redirect($this->referer()); > } > } > } > } I guess $this->redirect() should always been done in a controller? Or is there a way to catch it in the model? -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Hey Sam If you use return $this->redirect($this->referer()); or similar in your controller it will not require a view as it never tries to render one. On 27 July 2014 08:39, Sam Clauw wrote: > Okay, that was very helpful, thanks! I've moved the functionality to > AppModel now and it works. > One other additional problem: the application expects a view file now: > > *Error: *The view for *AttractionsController::**delete()* was not found. > > > How can you prevent it from loading a view file? :) > > -- > Like Us on FaceBook https://www.facebook.com/CakePHP > Find us on Twitter http://twitter.com/CakePHP > > --- > You received this message because you are subscribed to the Google Groups > "CakePHP" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to cake-php+unsubscr...@googlegroups.com. > To post to this group, send email to cake-php@googlegroups.com. > Visit this group at http://groups.google.com/group/cake-php. > For more options, visit https://groups.google.com/d/optout. > -- Kind Regards Stephen Speakman -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Okay, that was very helpful, thanks! I've moved the functionality to AppModel now and it works. One other additional problem: the application expects a view file now: *Error: *The view for *AttractionsController::**delete()* was not found. How can you prevent it from loading a view file? :) -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
Here's the link to the hasField method I forgot to paste http://api.cakephp.org/2.4/class-Model.html#_hasField So just to elaborate, in AppModel::beforeDelete() I check if the deleted field exists, if so I update the deleted field and return false. Otherwise I return true and let CakePHP do its thing. On 26 July 2014 10:22, Stephen S wrote: > If you want to do this with a behavior then you should look at the > beforeDelete callback, you can update your field within this callback and > then return false to automatically abort the delete (as per the book). > > > http://book.cakephp.org/2.0/en/models/behaviors.html#ModelBehavior::beforeDelete > > You could also just write something like this in your AppModel to make it > apply to all models, my applications use $this->hasField('deleted') to see > if soft delete is supported with the model. > > > > On 26 July 2014 09:22, Sam Clauw wrote: > >> I'll try to write my very first behavior that does a soft delete instead >> of a hard delete. I've expanded my model with >> >> public $actsAs = array('SoftDelete'); >> >> >> and I've created the behavior "SoftDeleteBehavior.php" in >> App/Model/Behavior: >> >> >> >>> >> App::uses('ModelBehavior', 'Model'); >>> class SoftDeleteBehavior extends ModelBehavior >>> { >>> function setup(Model $Model, $settings = array()) >>> { >>> >>> } >>> >>> function delete(Model $Model, $id = null) >>> { >>> $Model->id = $id; >>> >>> if ($Model->saveField('deleted', date('Y-m-d H:i:s'))) { >>> return true; >>> } >>> return false; >>> } >>> } >> >> >> However, when I try to delete something, the delete() method from my >> behavior doesn't overwrite the normal one. What could cause this issue? >> >> -- >> Like Us on FaceBook https://www.facebook.com/CakePHP >> Find us on Twitter http://twitter.com/CakePHP >> >> --- >> You received this message because you are subscribed to the Google Groups >> "CakePHP" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to cake-php+unsubscr...@googlegroups.com. >> To post to this group, send email to cake-php@googlegroups.com. >> Visit this group at http://groups.google.com/group/cake-php. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Kind Regards > Stephen Speakman > -- Kind Regards Stephen Speakman -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: Behavior method won't work
If you want to do this with a behavior then you should look at the beforeDelete callback, you can update your field within this callback and then return false to automatically abort the delete (as per the book). http://book.cakephp.org/2.0/en/models/behaviors.html#ModelBehavior::beforeDelete You could also just write something like this in your AppModel to make it apply to all models, my applications use $this->hasField('deleted') to see if soft delete is supported with the model. On 26 July 2014 09:22, Sam Clauw wrote: > I'll try to write my very first behavior that does a soft delete instead > of a hard delete. I've expanded my model with > > public $actsAs = array('SoftDelete'); > > > and I've created the behavior "SoftDeleteBehavior.php" in > App/Model/Behavior: > > > >> > App::uses('ModelBehavior', 'Model'); >> class SoftDeleteBehavior extends ModelBehavior >> { >> function setup(Model $Model, $settings = array()) >> { >> >> } >> >> function delete(Model $Model, $id = null) >> { >> $Model->id = $id; >> >> if ($Model->saveField('deleted', date('Y-m-d H:i:s'))) { >> return true; >> } >> return false; >> } >> } > > > However, when I try to delete something, the delete() method from my > behavior doesn't overwrite the normal one. What could cause this issue? > > -- > Like Us on FaceBook https://www.facebook.com/CakePHP > Find us on Twitter http://twitter.com/CakePHP > > --- > You received this message because you are subscribed to the Google Groups > "CakePHP" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to cake-php+unsubscr...@googlegroups.com. > To post to this group, send email to cake-php@googlegroups.com. > Visit this group at http://groups.google.com/group/cake-php. > For more options, visit https://groups.google.com/d/optout. > -- Kind Regards Stephen Speakman -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Behavior method won't work
I'll try to write my very first behavior that does a soft delete instead of a hard delete. I've expanded my model with public $actsAs = array('SoftDelete'); and I've created the behavior "SoftDeleteBehavior.php" in App/Model/Behavior: > App::uses('ModelBehavior', 'Model'); > class SoftDeleteBehavior extends ModelBehavior > { > function setup(Model $Model, $settings = array()) > { > > } > > function delete(Model $Model, $id = null) > { > $Model->id = $id; > > if ($Model->saveField('deleted', date('Y-m-d H:i:s'))) { > return true; > } > return false; > } > } However, when I try to delete something, the delete() method from my behavior doesn't overwrite the normal one. What could cause this issue? -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.