Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony][Symfony2][Doctrine2]Natywny sql problem z zapytaniem zwraca null
silverwind
post 31.01.2016, 12:35:24
Post #1





Grupa: Zarejestrowani
Postów: 80
Pomógł: 0
Dołączył: 8.02.2013

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


Dlaczego mi zwraca null przy natywny sql w zapytaniu. Dump w szablonie są same null.

  1. public function getExpensesByProperty()
  2. {
  3.  
  4. $rsm = new ResultSetMapping;
  5.  
  6. $rsm->addEntityResult('User\UserBundle\Entity\User', 'u');
  7.  
  8. $rsm->addFieldResult('u', 'username', 'username');
  9.  
  10. $sql = "
  11. SELECT username
  12. FROM users
  13.  
  14. ";
  15.  
  16.  
  17.  
  18.  
  19. $query= $this->_em->createNativeQuery(
  20. $sql, $rsm
  21. );
  22.  
  23.  
  24.  
  25. return $query->getResult();
  26.  
  27. }
  28. }
Go to the top of the page
+Quote Post
lukaskolista
post 31.01.2016, 12:41:35
Post #2





Grupa: Zarejestrowani
Postów: 872
Pomógł: 94
Dołączył: 31.03.2010

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


Hmm patrz na tą linijkę:
  1. $rsm->addEntityResult('User\UserBundle\Entity\User', 'u');

Mapujesz encję na alias "u", natomiast w SQL nigdzie tego aliasu nie używasz. SQL powinno wyglądać tak:
  1. SELECT u.*
  2. FROM users u
Nie pobieraj pojedynczych kolumn mapując dane na encje, zapomnisz czegoś zmapować i znowu będziesz się zastanawiał, czemu jest null.
Go to the top of the page
+Quote Post
silverwind
post 31.01.2016, 14:54:40
Post #3





Grupa: Zarejestrowani
Postów: 80
Pomógł: 0
Dołączył: 8.02.2013

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


Teraz mam problem ze SUMĄ . W ogóle ją nie wyświetla
  1. public function getExpensesByProperty()
  2. {
  3.  
  4. $rsm = new ResultSetMapping;
  5.  
  6. $rsm->addEntityResult('User\UserBundle\Entity\User', 'u');
  7.  
  8. $rsm->addFieldResult('u', 'username', 'username');
  9. $rsm->addFieldResult('u', 'id', 'id');
  10. $rsm->addJoinedEntityResult('Property\ManagementBundle\Entity\Owner' , 'o', 'u', 'owners');
  11. $rsm->addFieldResult('o', 'o_id', 'id');
  12. $rsm->addFieldResult('o', 'o_name', 'name');
  13. $rsm->addJoinedEntityResult('Property\ManagementBundle\Entity\Property' , 'p', 'o', 'property');
  14. $rsm->addFieldResult('p', 'p_id', 'id');
  15. $rsm->addFieldResult('p', 'p_name', 'name');
  16. $rsm->addFieldResult('p', 'p_adress', 'adress');
  17. $rsm->addJoinedEntityResult('Property\ManagementBundle\Entity\Expenses' , 'e', 'p', 'expenses');
  18. $rsm->addFieldResult('e', 'e_id', 'id');
  19. $rsm->addFieldResult('e', 'e_name', 'name');
  20. $rsm->addFieldResult('e', 'e.price', 'price');
  21. $sql = "
  22. SELECT u.id,u.username,
  23. o.id as o_id,o.name as o_name,
  24. p.id as p_id,p.name as p_name,p.adress as p_adress,
  25. e.id as e_id,e.name as e_name,SUM(e.price) as SUMA
  26. FROM users u
  27. INNER JOIN owner o ON u.id = o.user_id
  28. INNER JOIN property p ON o.id=p.owner_id
  29. LEFT JOIN expenses e ON p.id=e.property_id
  30. GROUP BY p_id
  31. ";
  32.  
  33.  
  34.  
  35.  
  36. $query= $this->_em->createNativeQuery(
  37. $sql, $rsm
  38. );
  39.  
  40. // $query->setParameter(1,$User);
  41.  
  42. return $query->getArrayResult();
  43.  
  44. }
  45. }
Go to the top of the page
+Quote Post
lukaskolista
post 31.01.2016, 16:09:56
Post #4





Grupa: Zarejestrowani
Postów: 872
Pomógł: 94
Dołączył: 31.03.2010

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


Pozwoliłem sobie poprawić:
  1. public function findExpensesByProperty()
  2. {
  3.  
  4. $rsm = new ResultSetMapping;
  5.  
  6. $rsm->addEntityResult('User\UserBundle\Entity\User', 'u');
  7. $rsm->addJoinedEntityResult('Property\ManagementBundle\Entity\Owner', 'o', 'u', 'owners');
  8. $rsm->addJoinedEntityResult('Property\ManagementBundle\Entity\Property' , 'p', 'o', 'property');
  9. $rsm->addJoinedEntityResult('Property\ManagementBundle\Entity\Expenses' , 'e', 'p', 'expenses');\
  10. $rsm->addScalarResult('SUMA', 'sum'); // W tablicy będzie pod kluczem z 2 argumentu, czyli sum
  11. $sql = "
  12. SELECT u.id,u.username,
  13. o.id as o_id,o.name as o_name,
  14. p.id as p_id,p.name as p_name,p.adress as p_adress,
  15. e.id as e_id,e.name as e_name,SUM(e.price) as SUMA
  16. FROM users u
  17. INNER JOIN owner o ON u.id = o.user_id
  18. INNER JOIN property p ON o.id=p.owner_id
  19. LEFT JOIN expenses e ON p.id=e.property_id
  20. GROUP BY p_id
  21. ";
  22.  
  23.  
  24.  
  25.  
  26. $query= $this->_em->createNativeQuery(
  27. $sql, $rsm
  28. );
  29.  
  30. // $query->setParameter(1,$User);
  31.  
  32. return $query->getArrayResult();
  33.  
  34. }
  35. }


1. Do metod, które służą do otrzymywania obiektów a które mogą zwrócić null używaj find, get zakłada, że otrzymasz obiekt i tyle, nie dopuszcza nulla (niepisana zasada).
2. Czemu korzystając z ORM rzeźbisz wszystko ręcznie?
3. Polecam lekturę: http://doctrine-orm.readthedocs.org/projec...native-sql.html

Ten post edytował lukaskolista 31.01.2016, 16:10:28
Go to the top of the page
+Quote Post
silverwind
post 31.01.2016, 19:12:15
Post #5





Grupa: Zarejestrowani
Postów: 80
Pomógł: 0
Dołączył: 8.02.2013

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


Potrzebuję takie zapytania zrobić ale widzę że nie jest to takie łatwe
  1. SELECT `users`.`username`,`owner`.`name`,`property`.`name` AS `p`,`property`.`adress`,`expenses`.`name` , SUM(`expenses`.`price`) AS `s`,`property`.`id` AS `i` FROM `users` INNER JOIN `owner` ON `users`.`id`=`owner`.`user_id`
  2. INNER JOIN `property`ON `owner`.`id`=`property`.`owner_id`
  3. LEFT JOIN `expenses`ON `property`.`id`=`expenses`.`property_id`
  4. GROUP BY `property`.`id`,`expenses`.`id`
  5. UNION SELECT `users`.`username`,`owner`.`name`,`property`.`name`,`property`.`adress`,`expenses`.`name`=NULL,SUM(`expenses`.`price`),`property`.`id` FROM `users`
  6. INNER JOIN `owner`ON `users`.`id`=`owner`.`user_id`
  7. INNER JOIN `property`ON `owner`.`id`=`property`.`owner_id`
  8. LEFT JOIN `expenses`ON `property`.`id`=`expenses`.`property_id`
  9. GROUP BY `property`.`id`
  10. ORDER BY `i`,`s`
Go to the top of the page
+Quote Post
lukaskolista
post 31.01.2016, 19:32:34
Post #6





Grupa: Zarejestrowani
Postów: 872
Pomógł: 94
Dołączył: 31.03.2010

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


Doctrine ORM (object-relational mapping) nie służy do "robienia zapytań", jest to obiektowa warstwa abstracji dla bazy danych i nie można tutaj myśleć kategoriami zapytań SQL, tylko obiektami i ich wzajemnymi relacjami. Mechanizmy Doctrine ORM nigdy nie wygenerują Ci takiego zapytania, bo nie o to tutaj chodzi.

Jeżeli chcesz pisać zapytania SQL zamiast korzystać z mapowania na obiektu, to użyj Doctrine DBAL (database abstraction layer), link do dokumentacji: http://docs.doctrine-project.org/projects/...dbal/en/latest/
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: 18.04.2024 - 02:25