根据实体输入Symfony2填充FormType

时间:2013-05-27 10:39:22

标签: symfony annotations doctrine

我有一个TeamMembers的实体,它有一个规范列表,可以为每个规范提供一个值。

但是如何获得完整的规范列表,每个规范后面都有一个字段,我可以在其中填写SpecificationValue。

该值将存储在SpecificationValue Entitiy中,其中ForeignKey存储到TeamMember和Specification。

所以我想要一个清单:[TeamMember edit_form>规格[]> SpecificationValue]

更多信息:

// FORM
class SpecificationValueType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('value')
            ->add('specification')
            ->add('teammember')
        ;
    }

// ENTITY
class SpecificationValue
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Specifications")
     * @ORM\JoinColumn(name="specification_id", referencedColumnName="id")
     */
    protected $specification; // refernce for specification entity > name, type[ENUM('input','textarea')]

     /**
     * @ORM\ManyToOne(targetEntity="Teammember")
     * @ORM\JoinColumn(name="teammember_id", referencedColumnName="id")
     */
    protected $teammember;

    /**
     * @var string
     * @ORM\Column(name="value", type="string", length=200, nullable=true)
     */
    protected $value;  // value that can be filled in for each 


// ENTITY
class TeamMember
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=250)
     */
    protected $name; // and some other fields, now skipped for the example

    /**
     * @ORM\OneToMany(targetEntity="SpecificationValue", mappedBy="specifications")
     */
    protected $specifications;

///

 /**
     * Add specificationValue
     *
     * @param \Foobar\MyBundle\Entity\SpecificationValue $specifications
     * @return SpecificationValue
     */
    public function addSpecification(\Foobar\MyBundle\Entity\SpecificationValue $specifications)
    {
        $this->specifications[] = $specifications;

        return $this;
    }

    /**
     * Remove specifications
     *
     * @param \Foobar\MyBundle\Entity\SpecificationValue $specifications
     */
    public function removeSpecification(\Foobar\MyBundle\Entity\SpecificationValue $specifications)
    {
        $this->specifications->removeElement($specifications);
    }

    /**
     * Get specifications
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSpecifications()
    {
        return $this->specifications;
    }

     /**
     * Get specifications
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSpecification()
    {
        return $this->specification;
    }

1 个答案:

答案 0 :(得分:0)

您正在寻找的是表单中的Entity field-type,以便能够从您的规范实体集合中进行选择。

  • 添加到您的TeamMember表单 - 键入规范类型的collection field-type(带有allow_add,prototype选项)

<强> TeamMemberType

->add('specifications', 'collection', array( 
    'type' => new SpecificationValueType(),
    'allow_add' => true, 
     // ... other options
  ))
  • 添加到您的specificationValue表单 - 键入规范的“实体”字段,并为相应值添加“值”字段( text?)。

<强> SpecificationValueType

->add('specification', 'entity', array(
      'class' => 'YourBundle:SpecificationValue',
      'property' => 'name',
      // ... other options
  ))
->add('value')