我遇到Symfony2的问题以及电子邮件地址集的验证。
实体\用户
/**
* 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;
}
}
实体\电子邮件
/**
* 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;
}
}
用户类型
/**
* 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';
}
}
断言注释包含在所有实体中。
问题是我可以在没有有效电子邮件地址的情况下保存表单。
An exception occurred while executing 'INSERT INTO email (email, user) VALUES (?, ?, ?)' with params [null, null]
< - 如果我使用“by_reference”= true我会收到此错误,如果“by_reference”为false我没有错误... 我希望你能帮助我:)。
答案 0 :(得分:1)
从终端运行此命令:
php app/console doctrine:schema:update --force