I have slugify method that slugify title attribute of entity class every time it change. so I put this method in entity class and call it like this:
public function setTitle($t){
$this->title = $t;
$this->slugTitle = $this->slugify($t);
}
it work fine for me, but if I have more than one class that use slugify method I should put this method in all of them, and this is code duplication.
so what should i do? if use helper class I cannot use slugify() like upper method :-(.
For a quick fix you can create some sort of
%Bundle%/Utils/Utils.php
class (which can be static), put the function in there and use it likeA more sophisticated approach would be using Gedmo's Doctrine Extensions. It handles that behaviour for you.
You can use a Doctrine event listener/subscriber to listen to entity changes and act on them.
services.xml:
However in this case, you're better off taking a look at the Doctrine Extensions bundle. It provides a Sluggable extension which can do this for you, rather than reinventing the wheel.
There is a package that you can you to do this for you.
It is called Sluggable behavior extension for Doctrine 2
Why not create a base class with your slugify function in (infact the whole setTitle could be in the base class if all your children have a title and needs slugging) and then inherit from it for all your entity classes?