Może trochę nie jasno się wyraziłem więc spróbuję raz jeszcze wszystko opisać.
Posiadam tabele:
User oraz MyCar
Tabela User i jej rekordy
Tabela MyCar i jej rekordy
id |
user_id |
car |
1 |
1 |
Fiat Punto |
2 |
1 |
Fiat 126p |
3 |
1 |
Fiat Panda |
Teraz plik User.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=250)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="MyCar", mappedBy="user")
*/
public $cars;
/**
* Constructor
*/
public function __construct()
{
$this->cars = new ArrayCollection();
}
Teraz plik MyCar.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="MyCar")
*/
class MyCar
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=250)
*/
private $car;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="cars")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
public $user;
W kontrolerze DefaultController.php
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getEntityManager();
$user = $em->getRepository("AppBundle:User")->findOneBy(array('id' => 1));
echo '<pre>';
\Doctrine\Common\Util\Debug::dump($user,15);
echo '</pre>';
die;
// replace this example code with whatever you need
return $this->render('default/index.html.twig', array(
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
));
}
Co jest nie tak i co chcę osiągnąć ?
Otóż w obiekcie
$user->getCars()
chciałbym mieć możliwość pobrania wszystkich aut danego Usera i faktycznie mam gdy wykonam:
echo $user->getCars()[0]->getCar();
Faktycznie zwraca mi pierwszy rekord w bazie.
Jest tylko jedno duże ALE
kiedy w kontrolerze wykonam kod:
echo '<pre>';
\Doctrine\Common\Util\Debug::dump($user->getCars(),15);
echo '</pre>';
Otrzymuje takie coś:
Array
(
[0] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\MyCar
[id] => 1
[car] => Fiat Punto
[user] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\User
[id] => 1
[name] => Alicja
[cars] => Array
(
[0] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\MyCar
[id] => 1
[car] => Fiat Punto
[user] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\User
[id] => 1
[name] => Alicja
[cars] => Array
(
[0] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\MyCar
[id] => 1
[car] => Fiat Punto
[user] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\User
[id] => 1
[name] => Alicja
[cars] => Array
(
[0] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\MyCar
[id] => 1
[car] => Fiat Punto
[user] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\User
[id] => 1
[name] => Alicja
[cars] => Array
(
[0] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\MyCar
[id] => 1
[car] => Fiat Punto
[user] => stdClass Object
(
[__CLASS__] => AppBundle\Entity\User
[id] => 1
[name] => Alicja
[cars] => Array(3)
)
)
I jest to zapętlone tyle razy, że gdybym zwiększył parametr MaxDepth w funkcji dump lub zrobił normalnego var_dump czy print_r to przeglądarka zawiesza się od ilości tekstu na ekranie.
Proszę o pomoc - o co chodzi ?