【问题标题】:How to set parent id on a child entity when submitting symfony3 form提交symfony3表单时如何在子实体上设置父ID
【发布时间】:2016-04-09 18:28:06
【问题描述】:

如何在 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

我收到此错误:

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;
    }
}

【问题讨论】:

    标签: doctrine-orm symfony


    【解决方案1】:

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

    回复者:Tuan nguyen

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

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

    类似问题:

    1. Doctrine: How to insert foreign key value

    以下功能您应该已经在您的 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 写入你的 Image 中。

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

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

    仅对关联的反面所做的更改将被忽略。 确保更新双向关联的双方(或在 从 Doctrine 的角度来看,至少是拥有方)就像我的情况一样 拥有方是我必须更新它的用户。教义 1 能够 自动管理它……太糟糕了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-08
      • 2019-12-11
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多