Replacing generated delete method in backend with Symfony
I have a bunch of models in my backend application in Symfony. Since these models are related between them through foreign keys, I would like to replace the default delete method with one of my own to check if an object can be deleted or not.
In order to do that I have developed an intermediate class MyActions, which inherits from sfActions, and set actions_base_class
with MyActions
in generator.yml.
In MyActions
class I have put a executeDeleteIfNotUsed
method, which I want to be executed instead of executeDelete
.
When it comes to configure the module actions I have made something like this in generator.yml
:
list:
title: Authorities
display: [name, updated_at]
fields:
name:
label: Name
updated_at:
label: Last update
date_format: f
sort: ~
object_actions:
_edit: ~
_delete: { label: Delete, action: delete_if_not_used }
But Symfony still generates routes to executeDelete
, instead of the one I have defined.
What am I doing wrong? What's the best way to implement this? Do I have to add custom routes besid开发者_运维百科es sfDoctrineRouteCollection?
Thanks!!
Seems to me like you're making this more complicated than it needs to be. I would just stick with the regular actions class, and override the delete method with your own custom one. This way you know your code is being used, and you don't have to mess around with the generator.yml
Edit
Based on your comment, I see why you want to do it the way explained in your question. Changing the action for the _delete
in generator.yml won't work. I'm not exactly sure why, but I'm fairly sure it has something to do with the fact that it's a Javascript request. Instead, add a new custom action.
list:
object_actions:
_edit: ~ # This ensures your edit action remains unchanged
delete_if_not_used: { label: Delete, action: action: delete_if_not_used }
Note that the actual action method and the action name in generator.yml must match. In your original question you were using the method executeDeleteIfNotUsed
and had delete_if_not_used
in generator.yml. This won't work because as far as Symfony knows, those are two completely different methods. I would suggest just using deleteifnotused
in generator.yml
I think you are not doing it in the right way. You just have put your delete logic in the delete() method of the Model. It's the best practice and recommanded way to do.
You have even nothing to change on the generated admin action, because it use the delete() method of your object:
if ($this->getRoute()->getObject()->delete())
{
$this->getUser()->setFlash('notice', 'The item was deleted successfully.');
}
Also, putting this logic in the Model will allow you to have a more consistent way to delete entity.
精彩评论