(Symfony版本2.7)Symfony2形式和多對多關係
嗨,我有一個與多對多的領域的形式問題。
Class Notification {
public function __construct()
{
$this->assigneduser = new \Doctrine\Common\Collections\ArrayCollection();
$this->flags = new ArrayCollection();
}
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Flag", inversedBy="notificationflags", cascade={"persist"})
* @ORM\JoinTable(name="sla_notificationflags",
* joinColumns={@ORM\JoinColumn(name="notification_id", referencedColumnName="notificationId")},
* inverseJoinColumns={@ORM\JoinColumn(name="flag_id", referencedColumnName="flagId")}
* )
*
*/
private $flags;
/**
* Add flag
*
* @param \AppBundle\Entity\Flag $flag
* @return Notification
*/
public function addFlag(Flag $flag)
{
$flag->addNotificationflag($this);
$this->flags[] = $flag;
return $this;
}
}
Class Flag {
public function __construct()
{
$this->notificationflags = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Notification", mappedBy="flags")
*/
protected $notificationflags;
/**
* Add notificationflags
*
* @param \AppBundle\Entity\Notification $notificationflag
* @return Flag
*/
public function addNotificationflag(Notification $notificationflag)
{
if(!$this->notificationflags->contains($notificationflag)) {
$this->notificationflags->add($notificationflag);
}
return $this;
}
}
我的Form類
class NotificationSingleFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('flags','entity',array(
'label' => false,
'attr' => array(
'class' => 'select'
),
'class' => 'AppBundle\Entity\Flag',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('p')
->addOrderBy('p.name','ASC');
},
'property' => 'name',
'required' => false
)
);
}
}
當我發送的形式,我看到的錯誤:
Neither the property "flags" nor one of the methods "addFlag()"/"removeFlag()", "setFlags()", "flags()", "__set()" or "__call()" exist and have public access in class "AppBundle\Entity\Notification".
Symfony的嘗試查找mandory方法來獲取標誌,添加或刪除標誌,但無法找到他們的實體內,所以你應該添加它們生成它們。你可以用cli來實現:php app/console doctrine:generate:entities –