I have a problem with validation. In Doctrine 1 i used this:
if ($model->isValid()) {
$model->save();
} else {
$errorStack = $model->getErrorStack();
...
}
and in $errorStack i got the column name and the error message. But in Doctrine 2 I can use just it:
Entity
/**
* @PrePersist @PreUpdate
*/
public function validate()
{
if ($this->name == null)) {
throw new \Exception("Name can't be null");
}
}
Controller:
try {
$user = new \User();
//$user->setName('name');
$user->setCity('London');
$this->_entityManager->persist($user);
$this->_entityManager->flush();
} catch(Exception $e) {
error_log($e->getMessage());
}
but I have two problems whit it:
- i don't know which column?
- i don't want to check unique manually
If i skip validate() from entity the unique will be catched (from this error.log)
Unique violation: 7 ERROR: duplicate key value violates unique constraint "person_email_uniq"
but for example the user save 2 records and the first is wrong, but the second valid, after the first save the EntityManager will close and I can't save the second(good) record because of "The EntityManager is closed".
Which is the best solution for this problem?