Symfony2.1.0にエンティティのコレクションを追加したい-私が得たdevバグ:
Neither property "sitterDegrees" nor method "getSitterDegrees()" nor method "isSitterDegrees()" exists in class "xxx\Entity\Degrees"
これは、DegreesFormType.phpにエンティティがあり
$this->form->bindRequest($request);
、ハンドラーのこの行にあるために発生します。「sitter」エンティティに複数の「度」を追加したい(ただし、度はhttp://symfony.com/doc/master/cookbook/form/form_collections.htmlとは異なる選択です)何かを忘れましたか?
エンティティ
SitterとDegreesの間の単純なManyToMany
シッター
class Sitter
{
//some properties
/**
* @var xxx\Entity\Degrees
* @ORM\ManyToMany(targetEntity="xxx\Entity\Degrees", orphanRemoval=true, inversedBy="sitters",cascade={"persist"})
* @ORM\JoinTable(name="sitter_degrees_relationships",
* joinColumns={
* @ORM\JoinColumn(name="sitter_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="degrees_id", referencedColumnName="id")
* }
* )
*/
private $sitterDegrees;
public function getSitterDegrees()
{
return $this->sitterDegrees;
}
public function setSitterDegrees(ArrayCollection $sitterDegrees)
{
foreach ($sitterDegrees as $sitterDegree) {
$sitterDegree->addSitter($this);
}
$this->sitterDegrees = $sitterDegrees;
}
public function addSitterDegree(xxx\Entity\Degrees $sitterDegrees)
{
$this->sitterDegrees[] = $sitterDegrees;
return $this;
}
public function removeSitterDegree(xxx\Entity\Degrees $sitterDegrees)
{
$this->sitterDegrees->removeElement($sitterDegrees);
}
}
度
class Degrees
{
public function __toString(){return $this->name;}
private $id;
private $name;
/**
* @var xxx\Entity\Sitter
* @ORM\ManyToMany(targetEntity="xxx\Entity\Sitter", mappedBy="sitterDegrees")
*/
private $sitters;
public function __construct()
{
$this->sitters = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function getSitters()
{
return $this->sitters;
}
public function addSitter(xxx\Entity\Sitter $sitter)
{
if (!$this->sitters->contains($sitter)) {
$this->sitters->add($sitter);
}
}
public function removeSitter(xxx\Entity\Sitter $sitters)
{
$this->sitters->removeElement($sitters);
}
}
FormType
VerifFormType.phpは私のメインフォームであり、DegreesFormTypeが埋め込まれています。
VerifFormType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
//->add('some_properties')
->add('sitterDegrees', 'collection', array(
'type' => new DegreesFormType(),
'by_reference' => false,
'allow_add' => true,
'allow_delete' => true,
)
);
}
DegreesFormType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('sitterDegrees', 'entity', array(
'class' => 'xxx:Degrees'
));
}
コントローラ
public function indexAction()
{
$user = $this->get('security.context')->getToken()->getUser();
$sitter = $user->sitter;
$formHandler = $this->get('xxx.form.handler');
$form = $formHandler->getForm();
$form->setData($sitter);
if ($formHandler->process()) {
//ok
}
//fail
}
ハンドラ
public function process()
{
$request = $this->container->get('request');
if ('POST' == $request->getMethod()) {
$this->form->bindRequest($request);//Fail at this line
if ($this->form->isValid()) {
return $this->onSuccess();
}
}
return false;
}
public function onSuccess()
{
$sitter = $this->form->getData();
$this->form->bindRequest($this->container->get('request'));
$sitter->setContainer($this->container);
$this->container->get('xxx.manager')->persistSitter($sitter);
return true;
}
index.html.twig
クックブックhttp://symfony.com/doc/master/cookbook/form/form_collections.htmlのようなJavaScriptを使用
<ul class="degrees" data-prototype="{{ form_widget(form.sitterDegrees.getVar('prototype')) | e }}">
{% for sitterDegree in form.sitterDegrees %}
<li>{{ form_row(sitterDegree) }}</li>
{% endfor %}
</ul>