Symfony2 relacje

0

Witam, mam problem z utworzeniem relacji.

Posiadam bazę danych User i chciałbym zrobić relacje do bazy ,,MyCar", ale coś mi nie wychodzi.

Tabela user posiada pola:

id
name

Tabela MyCar:

id
id_user
car

Przykładowe rekordy User:

id name
1 Alicja

Przykładowe rekordy MyCar:

id user_id car
1 1 Fiat Punto
2 1 Fiat 126p
3 1 Fiat Panda

I kiedy zrobiłem sobie relacje czytając dokumentacje: http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations

W User:

    /**
     * @ORM\OneToMany(targetEntity="MyCar", mappedBy="car")
     */
    protected $cars;

    public function __construct()
    {
        $this->cars = new ArrayCollection();
    }

 

W MyCars:

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="cars")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $category;
 

I teraz dajmy na to w kontrolerze wyszukuje Usera:

$em = $this->getDoctrine()->getEntityManager();
$user = $this->getRepository("AppBundle:User")->find("1");

Faktycznie w zmiennej:

$user->getCars()

Mam dostęp do wszystkich rekordów z tabeli MyCars, ale MyCars jest mapowane product_id do id usera i się robi zapętlenie zapytania.
Próbowałem sobie poradzić z http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html
ale jakoś nic mi z tego nie wyszło.

0

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

id name
1 Alicja

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 ?

1 użytkowników online, w tym zalogowanych: 0, gości: 1