1

How can I set the task_id on the TaskLine entity?

Similar Questions:

  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

I get this error:

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"

NOTE: Normally I work with PropelORM.

I'm trying to save a new TaskLine entity which is related to Task entity. I'm posting JSON payload which look something like this.

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

In the controller I json_decode the request payload and submit that to $form->submit($note_data), $form is an instance of:

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

Here is my Task entity

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 entity

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;
    }
}
Community
  • 1
  • 1
MurkyMuck
  • 126
  • 1
  • 10

1 Answers1

1

I found my answer in here: Best Practice for inserting objects with foreign keys in Symfony2

Answered by: Tuan nguyen

In ORM you have to set Foreign key by an object which your entity associated with. You could use EntityManager#getReference to get a reference to category entity without loading it from DB. Like this

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

Similar questions:

  1. Doctrine: How to insert foreign key value

Following function you should have already in your Slider entity (or similar).

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

return $this; } 

What it does is if you persist the entity it writes the ID of the Slider (sid) into your Image.

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

I found something in the Doctrine 2 documentation:

Changes made only to the inverse side of an association are ignored. Make sure to update both sides of a bidirectional association (or at least the owning side, from Doctrine’s point of view) As in my case the owning side is the User I must update it. Doctrine 1 was able to manage it automatically... too bad.

Community
  • 1
  • 1
MurkyMuck
  • 126
  • 1
  • 10