2015-10-25 116 views
0

(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".

+0

Symfony的嘗試查找mandory方法來獲取標誌,添加或刪除標誌,但無法找到他們的實體內,所以你應該添加它們生成它們。你可以用cli來實現:php app/console doctrine:generate:entities –

回答

0

你必須genrate getter/setter方法和添加/刪除方法的旗幟屬性。

使用php app/console doctrine:generate:entities自動

+0

我通常使用metod。並非所有代碼都粘貼到線程中。我有所有metod。我運行應用程序/控制檯原則:通用:實體。我不知道爲什麼表單不使用它們。 – Marcin