Doctrine2 __constructor not called when using $em->find(); ? How to load entity properly?
I'm pretty certain that find
or similar isn't expected to call the constructor...
You need to hook into the @PostLoad event.
Why would you want to call the constuctor of already persisted entity? When you need to validate it you should have done the validation or initializations before you have persisted it. So When you call a already persisted entity there is no point to validate it. The right place to put validation and other initializations is the constructor method of entity. Eg.
/**
* @Entity
*/
class User{
protected $name;
public function __construct($name) {
if (isset($name)) {
//** validate the name here */
$this->name=$name;
} else {
throw new Exception("no user name set!");
}
}
}
According to the doctrine2 documentation Doctrine2 never calls __construct() method of entities. http://www.doctrine-project.org/docs/orm/2.0/en/reference/architecture.html?highlight=construct