Mam dwa entities - User i Training.
Postanowiłem do nich dorobić model łączący Timesheet (kótry w założeniu będzie przechowywał informacje o płatności, przyjęciu zaproszenia itp.)

  1. <?php
  2. namespace Entity;
  3.  
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7.  
  8. /**
  9.  * @Gedmo\Tree(type="nested")
  10.  * @ORM\Table(name="tree")
  11.  * @ORM\Entity(repositoryClass="Entity\Repository\TreeRepository")
  12.  */
  13. class User
  14. {
  15. /**
  16.   * @ORM\Column(type="integer")
  17.   * @ORM\Id
  18.   * @ORM\GeneratedValue
  19.   */
  20. private $id;
  21.  
  22. /**
  23.   * @ORM\OneToMany(targetEntity="Entity\Timesheet", mappedBy="user", cascade={"persist", "remove"})
  24.   */
  25. private $timesheets;
  26.  
  27. public function __construct()
  28. {
  29. $this->timesheets = new ArrayCollection();
  30. }
  31.  
  32. public function getTimesheets()
  33. {
  34. return $this->timesheets;
  35. }
  36. ...


  1. <?php
  2. namespace Entity;
  3.  
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7.  
  8. /**
  9.  * @ORM\Entity
  10.  * @ORM\Table(name="trainings")
  11.  */
  12. class Training
  13. {
  14. /**
  15.   * @ORM\Column(type="integer")
  16.   * @ORM\Id
  17.   * @ORM\GeneratedValue
  18.   */
  19. private $id;
  20.  
  21. /**
  22.   * @ORM\OneToMany(targetEntity="Timesheet", mappedBy="training", cascade={"persist", "remove"})
  23.   */
  24. private $timesheets;
  25.  
  26. public function __construct()
  27. {
  28. $this->timesheets = new ArrayCollection();
  29. }
  30.  
  31. public function getTimeSheets()
  32. {
  33. return $this->timesheets;
  34. }
  35. ...


  1. <?php
  2. namespace Entity;
  3.  
  4. use Doctrine\ORM\Mapping as ORM;
  5. //use Doctrine\Common\Collections\ArrayCollection;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7.  
  8. /**
  9.  * @ORM\Entity
  10.  * @ORM\Table(name="timesheet")
  11.  */
  12. class Timesheet
  13. {
  14.  
  15. const REJECTED = -2;
  16. const DELAYED = -1;
  17. const PENDING = 0;
  18. const SUCCESS = 1;
  19.  
  20. /**
  21.   * @ORM\Id
  22.   * @ORM\GeneratedValue
  23.   * @ORM\Column(type="integer")
  24.   */
  25. private $id;
  26.  
  27. /**
  28.   * @ORM\ManyToOne(targetEntity="Entity\User", inversedBy="timesheets")
  29.   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  30.   **/
  31. private $user;
  32.  
  33. /**
  34.   * @ORM\ManyToOne(targetEntity="Entity\Training", inversedBy="timesheets")
  35.   * @ORM\JoinColumn(name="training_id", referencedColumnName="id")
  36.   **/
  37. private $training;


Mam utworzonych kilku userów i kilka trainingów ale kiedy używam poniższego kodu w utworzonej tabeli dla Timesheet pola user_id oraz training_id zawierają NULL. Bardzo proszę o pomoc dla czego tak się dzieje. Może robię coś po drodze nie tak?

  1. $sheet = new Entity\Timesheet();
  2. $user = $this->_helper->Em()->find('Entity\User', 1);
  3. $training = $this->_helper->Em()->find('Entity\Training', 1);
  4. $user->getTimesheets()->add($sheet);
  5. $training->getTimesheets()->add($sheet);
  6. $this->_helper->Em()->persist($sheet);
  7. $this->_helper->Em()->flush();


EDIT - PROBLEM ROZWIĄZANY

Trzeba wskazać obiekty w modelu Timesheets

Przykład:
  1. $sheet = new Entity\Timesheet();
  2. $user = $this->_helper->Em()->find('Entity\User', 4);
  3. $training = $this->_helper->Em()->find('Entity\Training', 2);
  4. $user->getTimesheets()->add($sheet);
  5. $training->getTimesheets()->add($sheet);
  6. $sheet->setUser($user);
  7. $sheet->setTraining($training);
  8. $this->_helper->Em()->persist($sheet);
  9. $this->_helper->Em()->flush();