【问题标题】:Symfony error on persisting object持久对象上的 Symfony 错误
【发布时间】:2017-01-17 10:30:57
【问题描述】:

我试图用 symfony 上的子表单制作一个简单的表单。我的问题是当我在控制器上坚持和反对时

$em->persist($professional);
foreach($form['degrees'] as $key){
  $degree = new ProfessionalDegree();
  $degree->setTitle($key['title']->getData());
  $degree->setPlace($key['place']->getData());
  $degree->setDateIn(new \DateTime($key['date_in']->getData()->format('Y-m-d')));
  $degree->setDateOut(new \DateTime($key['date_out']->getData()->format('Y-m-d')));
  $degree->setProfessional($professional);

  $em->persist($degree);
}
$em->flush()

数据库确实添加了两个字段,一个是专业为NULL,另一个是关系良好:

//更新//

Here is the ProfessionalExperience entity

/**
 * @ORM\Entity
 * @ORM\Table(name="professional_experience")
 */
class ProfessionalExperience
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    public function __construct()
    {
    }     

    /**
     * @ORM\Column(type="string")
     *
     */
    private $company;

    /**
     * @ORM\Column(type="string")
     *
     */
    private $position;

    /**
     * @ORM\Column(type="date")
     *
     */
    private $date_in;


    /**
     * @ORM\Column(type="date")
     *
     */
    private $date_out;

    /**
     * @ORM\Column(type="string", nullable=true)
     *
     */
    private $country;

    /**
     * @ORM\Column(type="string", nullable=true)
     *
     */
    private $city;

    /**
     * @ORM\Column(type="text")
     *
     */
    private $description;

    /**
     * @ORM\ManyToOne(targetEntity="Professional", inversedBy="experiences", cascade={"persist"})
     * @ORM\JoinColumn(name="professional_id", referencedColumnName="id")
     */
    private $professional;

...

这是我的专业实体

class Professional extends User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        $this->followers = new \Doctrine\Common\Collections\ArrayCollection();
        $this->following = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    private $name;

...

/**
     * @ORM\OneToMany(targetEntity="ProfessionalDegree", mappedBy="professional", cascade={"all"})
     */
    private $degrees;

// 已编辑 2 //

有我的表格:

    protected function editProfileFormProfessional($professional, $request) {

            $form = $this->createForm(ProfessionalType::class, $professional, [
            ... 
            $form->add('degrees', CollectionType::class, array(
                'entry_type'   => DegreeType::class,
                'allow_add'    => true,
                'allow_delete' => true,
            ));
if ($form->isSubmitted()) {

            if($form->isValid()){

                $em = $this->getDoctrine()->getManager();

                $professional->setName($form['name']->getData());
                $professional->setSurName($form['surname']->getData());
                $professional->setProfession($form['profession']->getData());
                $professional->setCountry($form['country']->getData());
                $professional->setProvince($form['province']->getData());
                $professional->setCity($form['city']->getData());
                $professional->setZipCode($form['zip_code']->getData());
                $professional->setBirthPlace($form['birth_place']->getData());
                $professional->setRefereeNumber($form['referee_number']->getData());
                $professional->setRefereePlace($form['referee_place']->getData());
                $professional->setAboutMe($form['about_me']->getData());

                $em->persist($professional);
                $em->flush();

             foreach($form['degrees'] as $key){
                $degree = new ProfessionalDegree();
                $degree->setTitle($key['title']->getData());
                $degree->setPlace($key['place']->getData());
                $degree->setDateIn(new \DateTime($key['date_in']->getData()->format('Y-m-d')));
                $degree->setDateOut(new \DateTime($key['date_out']->getData()->format('Y-m-d')));
                $degree->setProfessional($professional);

                $em->persist($degree);
               }
             $em->flush();

请注意,PorfesionalDegree 已级联所有,因为如果我不添加这些,则会出现持久级联错误。

// 已编辑 3

现在我知道错误在哪里,在所有集合之前调用句柄请求时,它自己添加了字段。 但问题是专业仍然是NULL,我已经修改了我的表格,添加了这个:

->add('degrees', CollectionType::class, array(
            'entry_type'   => DegreeType::class,
            'allow_add'    => true,
            'allow_delete' => true,
            'by_reference' => false,
        ));

将 by_reference 设置为 false,它应该只是这样做,但仍将专业设置为 NULL。我遵循了 Symfony 在其指南中所说的内容:

https://symfony.com/doc/current/form/form_collections.html

还是什么都没有

有人知道为什么吗? 谢谢

【问题讨论】:

  • 你可能不得不发布实体类文件。
  • 表格也很棒
  • 至少$form 内容,所以我们知道您的意见。模型和数据库结构也是如此。 $professional 来自哪里?它是否正确持久化?
  • 在foreach之前刷professional,可能是因为professional还没有id(需要保存才能有id)
  • @Erik 那里有我的实​​体

标签: php sql database symfony doctrine-orm


【解决方案1】:

我认为您不需要在 foreach 循环之前刷新。

要正确调试,您可以像这样观察循环中发生的情况:

foreach($form['degrees'] as $key){
            $degree = new ProfessionalDegree();
            $degree->setTitle($key['title']->getData());
            $degree->setPlace($key['place']->getData());
            $degree->setDateIn(new \DateTime($key['date_in']->getData()->format('Y-m-d')));
            $degree->setDateOut(new \DateTime($key['date_out']->getData()->format('Y-m-d')));
            $degree->setProfessional($professional);

            var_dump($degree->getId());
            var_dump($degree->getProfessional());

            $em->persist($degree);
           }

die();

在这里您将更好地了解发生了什么并根据需要进行调试。

请不要这样设置您的 $professional 对象。使用:

$form->handleRequest($request);

最后使用 FormType :http://symfony.com/doc/current/forms.html#creating-form-classes

祝你好运:)

【讨论】:

  • 谢谢你帮了大忙,现在我知道错在哪里了,我给我的代码添加了一个新的更新
  • 为了便于理解,我重新提出了一个新问题:stackoverflow.com/questions/41716039/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 2015-02-18
  • 2012-05-08
  • 2012-04-04
  • 2013-12-03
相关资源
最近更新 更多