Let’s say I have a User class :
$user = new User(1);
$user->setName('Bob'); // save "bob" to database with ID 1
$user->setGender('Male'); // save "male" to database with ID 1
echo $user->getName(); // display bob
echo $user->getGender(); // display "male";
echo $user->getDesignation() // display "Mr. Bob"
Now, in Symfony2, with Doctrine2, it seems that Entity is an object which is used to made link with the database. So I think all the setName(), setGender(), getName() & getGender() functions should go inside a file which is in the Entity directory of a Bundle (because those functions UPDATE or SELECT data from the database).
But what about getDesignation() ?
public function getDesignation() {
if ($this->getGender() == 'Male') return "Mr. ".$this->getName();
else return "Ms. ".$this->getName();
}
Is it OK to put a function which has absolutely no link with the database in an Entity ? Is it not a bad practice ?
Yes that’s okay. But look closely, it is still somewhat “linked” to the database as it makes use of the gender and name data which originally comes from the persistence layer (database).
That’s not at all bad practice, in fact it’s something very useful. It helps you to make use of the persistence layer in your model objects while decoupling your code from the database access.