如何在Symfony 2.1中使用集合中的实体(+ prototype)

时间:2012-07-10 13:24:35

标签: php doctrine symfony-2.1

我想在Symfony 2.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

使用一些javascript,如食谱http://symfony.com/doc/master/cookbook/form/form_collections.html

<ul class="degrees" data-prototype="{{ form_widget(form.sitterDegrees.getVar('prototype')) | e }}">
  {% for sitterDegree in form.sitterDegrees %}
     <li>{{ form_row(sitterDegree) }}</li>
  {% endfor %}
</ul>

1 个答案:

答案 0 :(得分:1)

错误来自DegreesFormType:sitterDegrees字段映射到setterDegrees类中的Degrees属性。但是,这个类没有这样的属性。

VerifFormType中存在类似的问题:sitterDegrees字段映射到setterDegrees类中的Sitter属性。但是,这个类没有这样的属性。