如何在提交symfony3表单时在子实体上设置父ID

时间:2016-04-09 18:28:06

标签: doctrine-orm symfony

如何在TaskLine实体上设置task_id?

类似的问题:

  1. Doctrine: How to insert foreign key value
  2. Best practice for inserting objects with foreign keys in symfony2
  3. Doctrine 2 entity association does not set value for foreign key
  4. 我收到此错误:

    Neither the property "task_id" nor one of the methods "getTaskId()", "taskId()", "isTaskId()", "hasTaskId()", "__get()" exist and have public access in class "AppBundle\Entity\TaskLine"
    

    注意:通常我使用PropelORM。

    我正在尝试保存与Task实体相关的新TaskLine实体。我发布的JSON有效负载看起来像这样。

    {
        "id": null,
        "task_id": 1,
        "note" : "new note"
    }
    

    在控制器中,我对请求有效负载进行json_decode并将其提交给$form->submit($note_data),$ form是以下实例:

    class TaskNoteType extends AbstractType
    {
        /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
            ->add( 'task_id', NumberType::class )
            ->add( 'note', TextType::class )
            ;
        }
    
        /**
         * @param OptionsResolver $resolver
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'AppBundle\Entity\TaskLine'
            ));
        }
    }
    

    这是我的任务实体

    class Task
    {
        /**
         * @var string
         *
         * @ORM\Column(name="description", type="string", length=150, nullable=true)
         */
        private $description;
    
     /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    }
    

    TaskLine实体

    class TaskLine
    {    
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @var \AppBundle\Entity\Task
         *
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Task")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="task_id", referencedColumnName="id")
         * })
         */
        private $task;
    
    
        /**
         * Get id
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set task
         *
         * @param \AppBundle\Entity\Task $task
         *
         * @return TaskLine
         */
        public function setTask(\AppBundle\Entity\Task $task = null)
        {
            $this->task = $task;
    
            return $this;
        }
    
        /**
         * Get task
         *
         * @return \AppBundle\Entity\Task
         */
        public function getTask()
        {
            return $this->task;
        }
    }
    

1 个答案:

答案 0 :(得分:1)

我在这里找到了答案: Best Practice for inserting objects with foreign keys in Symfony2

回答:Tuan nguyen

  

在ORM中,您必须通过实体的对象设置外键   有关联。您可以使用EntityManager#getReference来获取   引用类别实体而不从DB加载它。喜欢这个

$category = $entityManager->getReference('YourProject\Entity\Category', $categoryId);
$product = new Product();
$product->setCategory($category);

类似的问题:

  1. Doctrine: How to insert foreign key value
  2.   

    以下功能你应该已经在你的Slider实体中了(或者   相似)。

    public function addImage(Image $image) {
    $image->setSlider($this); // This is the line you're probably looking for
    $this->images[] = $image;
    
    return $this; } 
    
         

    它的作用是如果你坚持实体,它会将Slider(sid)的ID写入你的图像。

    1. Doctrine 2 entity association does not set value for foreign key
    2.   

      我在Doctrine 2文档中找到了一些东西:

           

      忽略仅对关联的反面进行的更改。   确保更新双向关联的两侧(或在   至少是拥有方,从学说的角度来看)就像我的情况一样   拥有方是用户我必须更新它。学说1能够   自动管理......太糟糕了。