Zamówienie składa się z wielu ZamowienieProdukt. Czy mając obiekt Zamowienie mogę bez pętli wyciągnąć wszystkie jego produkty gdy jest relacja OneToMany? Bo poniższy sposób zwraca pustą zmienną $produkty
//Kontroler
$zamowienie = $this->getDoctrine()
->getRepository('AppBundle:Zamowienie')
->find(157);
$produkty = $zamowienie->getZamowienieProdukty();
(IMG:
http://i66.tinypic.com/71qzoi.jpg)
/**
* @ORM\Table(name="zamowienie", indexes={@ORM\Index(name="idKlient_idx", columns={"idKlient"}), @ORM\Index(name="idStatus_idx", columns={"idStatus"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\ZamowienieRepository")
*/
class Zamowienie
{
...
/**
* @ORM\OneToMany(targetEntity="ZamowienieProdukt", mappedBy="idzamowienie")
*/
protected $zamowienie_produkty;
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getZamowienieProdukty()
{
return $this->zamowienie_produkty;
}
/**
* @ORM\Table(name="zamowienie_produkt", indexes={@ORM\Index(name="idZamowienie_idx", columns={"idZamowienie"}), @ORM\Index(name="isbn_idx", columns={"isbn"})})
* @ORM\Entity
*/
class ZamowienieProdukt
{
/**
* @var \AppBundle\Entity\Ksiazka
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Ksiazka", inversedBy="zamowienie_produkty")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="isbn", referencedColumnName="isbn")
* })
*/
private $isbn;
/**
* @var \AppBundle\Entity\Zamowienie
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Zamowienie", inversedBy="zamowienie_produkty")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="idZamowienie", referencedColumnName="idZamowienie")
* })
*/
private $idzamowienie;
(nie wiem, może to pytanie wyjątkowo durne, ale ciężko mi czasem rozpoznać co dzieje się magicznie bez mojej wiedzy, a o co muszę sam zadbać)
---edit----
czy to trzeba
takie cholerstwo konstruować z 6 metodami, czy ten człowiek robi to tak jak należy, czy jest jakiś prostszy sposób?
/**
* @ORM\Entity
*/
class User extends BaseUser
{
/**
* @var Collection
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Employee", mappedBy="user", cascade="persist")
*/
protected $employees;
public function __construct()
{
$this->employees = new \Doctrine\Common\Collections\ArrayCollection();
parent::__construct();
}
public function prePersist($user)
{
parent::prePersist($user);
$user->setEmplolyees($user->getEmployees());
}
public function preUpdate($user)
{
parent::preUpdate($user);
$user->setEmplolyees($user->getEmployees());
}
/**
* Add employees
*
* @param \AppBundle\Entity\User $employee
* @return Organisation
*/
public function addEmployee(\AppBundle\Entity\User $employee)
{
$employee->setOrganisation($this);
$this->employees[] = $employee;
return $this;
}
function setEmployees($employees)
{
foreach ($employees as $employee)
{
$this->addEmployee($employee);
}
}
/**
* Remove employee
*
* @param \AppBundle\Entity\User $employee
*/
public function removeEmployee(\AppBundle\Entity\User $employee)
{
$this->employees->removeElement($employee);
}
/**
* Get employees
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getEmployees()
{
return $this->employees;
}
}
Ten post edytował porzeczki 15.10.2016, 21:49:44