Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [S3][ORM] Join tej samej tabeli?
markuz
post 20.07.2017, 21:40:18
Post #1





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


Cześć,

Potrzebuję zrobić query które wyciągnie mi lokalizacje razem z sublokalizacjami. Tabela location wygląda mniej więcej tak:
Kod
- id
- parent_id (może być NULL)
- name
- slug


Coś takiego mi wyciąga samych parentów (gdzie parent_id = null):

  1. $this->createQueryBuilder('l')
  2. ->select('l.name, l.slug')
  3. ->where('l.parent is null')
  4. ->getQuery()
  5. ->getArrayResult();


Da się w ORM zrobić tak, żeby np. pod kluczem "sublocations" wyciągneło mi dodatkowo lokalizacje gdzie sublocation.parent_id = location.id?
Wiem, że mógłbym zrobić foreach i dla każdego wyciągać sub budując tablice ale chciałbym to zrobić "po bożemu" smile.gif


--------------------
Go to the top of the page
+Quote Post
ohm
post 20.07.2017, 22:56:28
Post #2





Grupa: Zarejestrowani
Postów: 618
Pomógł: 143
Dołączył: 22.12.2010

Ostrzeżenie: (0%)
-----


Da się, robisz po prostu OneToMany i ManyToOne w jednym entity i tyle, reszta kwestia zapytania.
Go to the top of the page
+Quote Post
markuz
post 20.07.2017, 23:21:28
Post #3





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


Mam coś takiego:

  1. /**
  2. * @ORM\ManyToOne(targetEntity="Location", inversedBy="subLocations")
  3. * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
  4. */
  5. private $parent;
  6.  
  7. /**
  8. * @ORM\OneToMany(targetEntity="Location", mappedBy="parent")
  9. */
  10. private $subLocations;


Nie potrafię skleić odpowiedniego zapytania w query builder - jednak poświęciłem dopiero kilkanaście minut na to, jak masz jakąś wskazówkę to chętnie przeczytam smile.gif W przeciwnym wypadku jutro pewnie sam do tego jakoś dojdę.

Ten post edytował markuz 20.07.2017, 23:22:00


--------------------
Go to the top of the page
+Quote Post
ohm
post 21.07.2017, 08:18:59
Post #4





Grupa: Zarejestrowani
Postów: 618
Pomógł: 143
Dołączył: 22.12.2010

Ostrzeżenie: (0%)
-----


Coś w stylu
  1. $this->createQueryBuilder('l')
  2. ->select('l, sl')
  3. ->leftjoin("l.subLocations", "sl")
  4. ->where('l.parent is null')
  5. ->getQuery()
  6. ->getArrayResult();
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.04.2024 - 21:30