在嵌入表单

时间:2015-12-28 13:59:11

标签: php forms symfony doctrine-orm one-to-one

我的symfony EntityBundle中有这些实体: 拳头Organization.php

    class Organization
    {
    /**
         * @var integer
         *
         * @ORM\Column(name="organization_id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        protected $id;

        /**
         * @Assert\Type(
         *     type="string",
         *     message="The value {{ value }} is not a valid {{ type }}."
         * )
         * @Assert\NotNull()
         * @Assert\NotBlank()
         * @ORM\Column(name="organization_name", length=255)
         */
        protected $name;
    /**
         * @var Address
         *
         * @ORM\OneToOne(targetEntity="Address", inversedBy="organization", cascade={"persist"})
         * @ORM\JoinColumn(name="address_id", referencedColumnName="address_id")
         */
        protected $address;
    /**
         * Set address
         *
         * @param \MyNamespace\EntityBundle\Entity\Address $address
         *
         * @return Organization
         */
        public function setAddress(\MyNamespace\EntityBundle\Entity\Address $address = null)
        {
            $this->address = $address;
            $address->setOrganization($this);

        }


        /**
         * Get address
         *
         * @return \MyNamespace\EntityBundle\Entity\Address
         */
        public function getAddress()
        {
            return $this->address;
        }
    }

其次,Address.php

class Address
{
/**
     * @var integer
     *
     * @ORM\Column(name="address_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @Assert\Length(
     *      max = 5,
     *      maxMessage = "Votre numéro de rue doit avoir maximum {{ limit }} chiffres"
     * )
     * @Assert\NotNull()
     * @Assert\NotBlank()
     * @ORM\Column(name="street_number", type="integer")
     */
    protected $streetNumber;
/**
     * @Assert\Type(
     *     type="string",
     *     message="The value {{ value }} is not a valid {{ type }}."
     * )
     * @Assert\NotNull()
     * @Assert\NotBlank()
     * @ORM\Column(name="street_name", length=255)
     */
    protected $streetName;
/**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\OneToOne(targetEntity="Organization", mappedBy="address")
     */
    protected $organization;
/**
     * Set organization
     *
     * @param \MyNamespace\EntityBundle\Entity\Organization $organization
     *
     * @return Address
     */
    public function setOrganization(\MyNamespace\EntityBundle\Entity\Organization $organization = null)
    {
        $this->organization = $organization;

        return $this;
    }

    /**
     * Get organization
     *
     * @return \MyNamespace\EntityBundle\Entity\Organization
     */
    public function getOrganization()
    {
        return $this->organization;
    }

    /**
     * Add organization
     *
     * @param \MyNamespace\EntityBundle\Entity\Organization $organization
     * @return Address
     */
    public function addOrganization(\MyNamespace\EntityBundle\Entity\Organization $organization)
    {
        $this->organization[] = $organization;

        $organization->setOrganization($this);

        return $this;
    }
}

这是y控制器,以便以嵌入形式保存数据:

public function RegistrationAction()
    {

      $organization = new Organization();

      $form = $this->createForm(new OrganizationType(), $organization)
                   ->add('save', 'submit', array('label' => 'Create'));

      $request = $this->getRequest();

      if( $request->isMethod('POST') ) {

        $form->bind($request);

        if( $form->isValid() ) {

          $em = $this->getDoctrine()->getManager();
          $em->persist($organization);
          $em->flush();

           return $this->redirectToRoute('homepage');

        }
      }

      return $this->render('MyBundle:MyFolder:Registration.html.twig', array(
        'form' => $form->createView(),
      ));
    }

以下是我的表单类型OrganizationType.php

$builder
->add('name')
/* ... the other fields ... */
->add('address', 'collection', array(
                                     'type' => new AddressType(),
                                     'allow_add' => true,
                                     'allow_delete' => false,
                                     'by_reference' => false,
                                     'mapped' => true,
                                     ));
}

最后我的树枝视图渲染表格:

<div>
  {{ form_start(form, {'action': path('path_action'), 'method': 'POST'}) }}
    {{ form_errors(form) }}
    {{ form_row(form.name) }}
    {# .. the other fields ...#}
    {{ form_row(form.address.vars.prototype) }}
  {{ form_end(form) }}
</div>

除非我提交表单,否则一切正常。 发生此错误:

  

捕获致命错误:参数1传递给   MyNamespace \ EntityBundle \ Entity \ Organization :: setAddress()必须是   MyNamespace \ EntityBundle \ Entity \ Address的实例,给出的数组,   呼唤   C:\瓦帕\ WWW \对myApp \厂商\ symfony的\ symfony的\ SRC \的Symfony \元器件\ PropertyAccess \ PropertyAccessor.php   在502行并定义,   &#39; C:\瓦帕\ WWW \对myApp \ SRC \ myNameSpace对象\ EntityBundle \实体\ Organization.php&#39 ;,   &#39; 336&#39;,数组(&#39;此&#39; =&gt;对象(组织)))in   第336行的src \ MyNamespace \ EntityBundle \ Entity \ Organization.php

我该如何解决?

1 个答案:

答案 0 :(得分:1)

问题出在这里

->add('address', 'collection', array(
                                 'type' => new AddressType(),
                                 'allow_add' => true,
                                 'allow_delete' => false,
                                 'by_reference' => false,
                                 'mapped' => true,
                                 ));

即使是一个oneToOne关系,你已经将表单属性类型设置为一个集合,所以当symfony解析表单数据时,它将地址包装在一个数组中,你可以通过以下方法修复:

->add('address', new AddressType());