I'm learning doctrine2, and having a problem how to call constructor automatically. For example, in my entity I have
/**
* @Entity
*/
class User{
....
public function __construct() {
exit('in');
}
}
and when I get the object this way:
$userObj = $em->find('User', 1);
I do get that object from database, but constructor is never called. I want to put some common things in constructor, like validation rules, or even to put sample code from the doctrine documentation like
$this->comments = new ArrayCollection();
This ofcourse works when I create new object in code for creating a user like
$user = new User(); //now constructor works just fine
Now, what is the "proper" way of getting the entity? I doubt I have to call constructor manually each time I user $em->find() with $user0bj->__construct(); ? This would kinda sucks then... Or I should use something other then ->find() to get single entity properly? I know I can user @PrePersist, and I am using it to actually do validation checks etc. I guess that I'm probably missing something here, or I'm trying to use constructor in a poor way. Thanks for any explanations and guides!