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.)
<?php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @Gedmo\Tree(type="nested")
* @ORM\Table(name="tree")
* @ORM\Entity(repositoryClass="Entity\Repository\TreeRepository")
*/
class User
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Entity\Timesheet", mappedBy="user", cascade={"persist", "remove"})
*/
private $timesheets;
public function __construct()
{
$this->timesheets = new ArrayCollection();
}
public function getTimesheets()
{
return $this->timesheets;
}
...
<?php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity
* @ORM\Table(name="trainings")
*/
class Training
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Timesheet", mappedBy="training", cascade={"persist", "remove"})
*/
private $timesheets;
public function __construct()
{
$this->timesheets = new ArrayCollection();
}
public function getTimeSheets()
{
return $this->timesheets;
}
...
<?php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
//use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity
* @ORM\Table(name="timesheet")
*/
class Timesheet
{
const REJECTED = -2;
const DELAYED = -1;
const PENDING = 0;
const SUCCESS = 1;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Entity\User", inversedBy="timesheets")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
**/
private $user;
/**
* @ORM\ManyToOne(targetEntity="Entity\Training", inversedBy="timesheets")
* @ORM\JoinColumn(name="training_id", referencedColumnName="id")
**/
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?
$sheet = new Entity\Timesheet();
$user = $this->_helper->Em()->find('Entity\User', 1);
$training = $this->_helper->Em()->find('Entity\Training', 1);
$user->getTimesheets()->add($sheet);
$training->getTimesheets()->add($sheet);
$this->_helper->Em()->persist($sheet);
$this->_helper->Em()->flush();
EDIT - PROBLEM ROZWIĄZANY
Trzeba wskazać obiekty w modelu Timesheets
Przykład:
$sheet = new Entity\Timesheet();
$user = $this->_helper->Em()->find('Entity\User', 4);
$training = $this->_helper->Em()->find('Entity\Training', 2);
$user->getTimesheets()->add($sheet);
$training->getTimesheets()->add($sheet);
$sheet->setUser($user);
$sheet->setTraining($training);
$this->_helper->Em()->persist($sheet);
$this->_helper->Em()->flush();
Ten post edytował ze4lot 4.12.2012, 16:57:58