Reputation: 3255
i have a problem with Symfony2 and the validation of an email addresses collection.
Entity\User
/**
* User
*
* @ORM\Table()
* @ORM\Entity
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var Email[]
* @ORM\OneToMany(targetEntity="Email", mappedBy="user", cascade={"all"}, orphanRemoval=true)
* @Assert\Valid
*/
private $emails;
public function __construct()
{
$this->emails = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId()
{
return $this->id;
}
/**
* Add emails
*
* @param Email $emails
* @return User
*/
public function addEmail(Email $emails)
{
if($emails->getEmail() !== null && strlen($emails->getEmail()) > 0)
{
$emails->setUser($this);
$this->emails[] = $emails;
}
return $this;
}
/**
* Remove emails
*
* @param Email $emails
*/
public function removeEmail(Email $emails)
{
$emails->setUser();
$this->emails->removeElement($emails);
}
/**
* Set emails
*
* @param Email[] $emails
* @return User
*/
public function setEmails($emails)
{
$this->emails = array();
foreach($emails as $email)
{
$this->addEmail($email);
}
return $this;
}
/**
* Get emails
*
* @return Email[]
*/
public function getEmails()
{
return $this->emails;
}
}
Entity\Email
/**
* Email
*
* @ORM\Table()
* @ORM\Entity
*/
class Email
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="email", type="string", length=255)
* @Assert\Email(checkMX = false)
*/
private $email;
/**
* @var User
* @ORM\ManyToOne(targetEntity="User", inversedBy="emails")
* @ORM\JoinColumn(name="user", referencedColumnName="id", onDelete="CASCADE")
* @Assert\NotNull()
*/
private $user;
public function getId()
{
return $this->id;
}
/**
* Set email
*
* @param string $email
* @return Email
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set user
*
* @param User $user
* @return Email
*/
public function setUser(User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return User
*/
public function getUser()
{
return $this->user;
}
}
UserType
/**
* Class UserType
*/
class UserType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('emails', 'collection', array(
'type' => new EmailType(),
'allow_add' => true,
'allow_delete' => true
))
;
}
public function getName()
{
return 'user';
}
}
EmailType
/**
* Class EmailType
*/
class EmailType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Entity\User'
));
}
public function getName()
{
return 'email';
}
}
The assert annotation is included in all entities.
The problem is that i can save the form without a valid email address.
An exception occurred while executing 'INSERT INTO email (email, user) VALUES (?, ?, ?)' with params [null, null]
<- If i use "by_reference" = true i get this error and if "by_reference" is false i have no error...I hope you could help me :)
Upvotes: 0
Views: 71
Reputation: 3849
Run this command from your terminal:
php app/console doctrine:schema:update --force
Upvotes: 1