私の問題:フォームビルダーにタイプエンティティのコレクションを埋め込むことが可能かどうかを知りたいです。
私は2つのエンティティを持っています:Partitions
多くのエンティティを取得したGenrepartition
エンティティ
/**
* Partitions
*
* @ORM\Table(name="partitions")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Partitions
{
// ...
/**
* @var \Genrepartition
*
* @ORM\OneToMany(targetEntity="Genrepartition", mappedBy="idGenre")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="idGenre", referencedColumnName="idGenre")
* })
*/
private $idgenre;
// ...
/**
* Set idgenre
*
* @param \Acme\PartBundle\Entity\Genrepartition $idgenre
* @return Partitions
*/
public function setIdgenre(\Acme\PartBundle\Entity\Genrepartition $idgenre = null)
{
$this->idgenre = $idgenre;
return $this;
}
/**
* Get idgenre
*
* @return \Acme\PartBundle\Entity\Genrepartition
*/
public function getIdgenre()
{
return $this->idgenre;
}
/**
* Constructor
*/
public function __construct()
{
$this->idgenre = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add idgenre
*
* @param \Acme\PartBundle\Entity\Genrepartition $idgenre
* @return Partitions
*/
public function addIdgenre(\Acme\PartBundle\Entity\Genrepartition $idgenre)
{
$this->idgenre[] = $idgenre;
return $this;
}
/**
* Remove idgenre
*
* @param \Acme\PartBundle\Entity\Genrepartition $idgenre
*/
public function removeIdgenre(\Acme\PartBundle\Entity\Genrepartition $idgenre)
{
$this->idgenre->removeElement($idgenre);
}
でPartitionsType
、コレクションを埋め込むための2つの方法、最初の「ドキュメントスタイル」を試しました。
->add('idgenre', "collection", array("type" => new GenrepartitionType()))
そして2番目にこのコード:
->add("idgenre", "entity", array(
"class" => "AcmePartBundle:Genrepartition",
"property" => "libellegenre",
"query_builder" => function(\Doctrine\ORM\EntityRepository $er) {
return $er->createQueryBuilder("u");
}
しかし、問題は、コレクションのコレクションが必要なことです。
そのように埋め込むことが可能かどうか疑問に思いました:
->add("idgenre", "collection", array("class" => "entity" ...
現在、私は2番目の方法を使用してbuildForm
おりController
、コレクションのコレクションを自分で追加しましたが、小枝を使用してレンダリングしようとしましたが、成功しませんでした。
これが私のコントローラーです:
$Partition = new \Acme\PartBundle\Entity\Partitions();
//...
$em = $this->getDoctrine()
->getRepository("AcmePartBundle:Genrepartition")->findAll();
foreach($em as $value) $Ajouter->getIdpartition()->addIdgenre($value);
// ...
そして、これが小枝ファイルです:
<select id=".." name="..">
{% for elem in formPart.idpartition.idgenre %}
<option value="{{ elem.vars.value.idgenre }}">{{ elem.vars.value.libellegenre }}</option>
{% endfor %}
</select>