There are 2 entities Courses and UserTask. They have OneToMany relation, respectively. For some reason, when a user logs in and makes a task in my application, the course id is not set to database, although the Course and UserTask are connected. In this case, all other fields, for example userId, are set. And the code is absolutely identical to the working examples. If you manually save the course id to the database, it works but I need this courseId to automatically set when user enter some task. Tell me please maybe I missed something? As you can see on screenshot i have all fields filled but courseId is empty Database image
This is my Courses Entity
/** * @ORM\OneToMany( targetEntity="UserTask", mappedBy="courses", cascade={"persist"}, orphanRemoval=true ) */ private $userTask; public function __construct() { $this->userTask = new \Doctrine\Common\Collections\ArrayCollection(); } /** * @return ArrayCollection */ public function getUserTask() { return $this->userTask; } /** * @param mixed $userTask * @return Courses */ public function setUserTask($userTask) { if (count($userTask) > 0) { foreach ($userTask as $i) { $this->addUserTask($i); } } return $this; } /** * @param UserTask $userTask */ public function addUserTask(UserTask $userTask) { $userTask->setCourses($this); $this->userTask->add($userTask); } /** * @param UserTask $userTask */ public function removeUserTask(UserTask $userTask) { $this->userTask->removeElement($userTask); }
This is my UserTasks Entity
/** * @ORM\ManyToOne(targetEntity="Courses", inversedBy="userTask") */ private $courses; /** * @param Courses $courses * @return UserTask */ public function setCourses(Courses $courses) { $this->courses = $courses; return $this; } /** * @return Courses */ public function getCourses(): ?Courses { return $this->courses; }