Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony] odwołanie do Doctrine w niezależnej klasie
dopelganger
post 10.03.2016, 14:37:49
Post #1





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


cześć
mam klasę w której są różne pomocnicze metody, ale za piernika nie wiem w jaki sposób odwołać się do Doctrine,,,
Wywala błąd:/

Attempted to call an undefined method named "getDoctrine" of class "MyBundle\Entity\Helper".

  1. <?php
  2.  
  3. namespace MyBundle\Entity;
  4.  
  5. class Helper {
  6.  
  7. // ....
  8.  
  9. public function Cos() {
  10. return $this->getDoctrine()->getManager()->createQuery("zapytanie")->getResult();
  11. }
  12.  
  13. }


Prosze o pomoc.
Go to the top of the page
+Quote Post
ohm
post 10.03.2016, 14:41:18
Post #2





Grupa: Zarejestrowani
Postów: 623
Pomógł: 144
Dołączył: 22.12.2010

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


Możesz przekazać doctrine przez konstruktor, lub przez Dependancy Injection
Go to the top of the page
+Quote Post
dopelganger
post 11.03.2016, 08:19:41
Post #3





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


dodałem tak:

  1. private $em;
  2.  
  3. public function __construct($em) {
  4. $this->em = $em;
  5. }


ale wywala dalej błąd:

Attempted to call an undefined method named "getDoctrine"
Go to the top of the page
+Quote Post
kapslokk
post 11.03.2016, 08:22:48
Post #4





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


Skoro wstrzyknąłeś entity manager, to zamień
  1. public function Cos() {
  2. return $this->getDoctrine()->getManager()->createQuery("zapytanie")->getResult();
  3. }

na
  1. public function Cos() {
  2. return $this->em->createQuery("zapytanie")->getResult();
  3. }
Go to the top of the page
+Quote Post
dopelganger
post 11.03.2016, 08:26:22
Post #5





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


Cytat(kapslokk @ 11.03.2016, 08:22:48 ) *
Skoro wstrzyknąłeś entity manager, to zamień
  1. public function Cos() {
  2. return $this->getDoctrine()->getManager()->createQuery("zapytanie")->getResult();
  3. }

na
  1. public function Cos() {
  2. return $this->em->createQuery("zapytanie")->getResult();
  3. }



dziękuje!
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: 16.08.2025 - 06:03