【问题标题】:Error when persisting OneToOne relation in embed form以嵌入形式保持 OneToOne 关系时出错
【发布时间】:2015-12-28 13:59:10
【问题描述】:

我的 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:\wamp\www\myApp\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php 在第 502 行并定义, 'C:\wamp\www\myApp\src\MyNamespace\EntityBundle\Entity\Organization.php', '336', array('this' => object(Organization))) in src\MyNamespace\EntityBundle\Entity\Organization.php 在第 336 行

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案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());
    

    【讨论】:

    • 所以我的错误是将这个 OneToOne 关系视为一个集合字段,对吗?另一个问题,你的回答很好,现在问题是嵌入表单地址的标签,它也显示出来了,我可以像你在我的另一个问题here 中建议的那样用数据原型删除他吗?顺便说一句,非常感谢。
    • 是的,问题出在你的集合字段类型中 其他问题你使用了 symfony 预定义的默认视图,你只需要使用嵌入的表单原型
    • 你摇滚并拯救了我的一天!非常感谢您的回答。
    • 欢迎您的朋友,您可以将您的其他问题标记为已解决
    猜你喜欢
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    相关资源
    最近更新 更多