Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SF][SF2][Symfony2] Autocomplete problem z obiektem
aras785
post 30.06.2015, 09:04:34
Post #1





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

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


Witam.

Testuję sobie pakiet: https://github.com/PUGX/PUGXAutoCompleterBu...es/doc/index.md
I zrobiłem wszystko według instrukcji ale niestety dostaję błąd
Cytat
Error: Call to a member function getId() on a non-object


Oto pliki:

CompanyController:

  1. /**
  2.   * @Route("/company/create", name="admin_company_create")
  3.   */
  4. public function create(Request $request)
  5. {
  6.  
  7. $company = new Company();
  8. $form = $this->createFormBuilder($company)
  9. ->add('email','text',array('label'=>'Email'))
  10. ->add('name','text',array('label'=>'Nazwa'))
  11. ->add('city','autocomplete',array('label'=>'Testowe','class'=>'AcmeAdminBundle:BrandDevice'))
  12. ->add('phone','text',array('label'=>'Telefon'))
  13. ->add('nip','text',array('label'=>'NIP'))
  14. ->add('website','text',array('label'=>'Website'))
  15. ->add('save', 'submit', array('label' => 'Zapisz'))
  16. ->getForm();
  17.  
  18. $form->handleRequest($request);
  19. if($form->isValid())
  20. {
  21. $company->setDateAdd(new \DateTime());
  22. $company->setDateUpdate(new \DateTime());
  23. $em = $this->getDoctrine()->getManager();
  24. $em->persist($company);
  25. $em->flush();
  26. $this->addFlash('success','Dodano firmę');
  27. return $this->redirectToRoute('admin_company_edit',array('id'=>$company->getId()));
  28. }
  29. //title
  30. $this->view_data['title']['subheader'] = 'Dodaj';
  31. $this->view_data['form'] = $form->createView();
  32. return $this->render('AcmeAdminBundle:Company:create.html.twig', $this->view_data);
  33. }


BrandDevice:
  1. <?php
  2.  
  3. namespace Acme\Bundle\AdminBundle\Entity;
  4.  
  5. use Cocur\Slugify\Slugify;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8.  
  9. /**
  10.  * BrandDevice
  11.  *
  12.  * @ORM\Table(name="brand_device")
  13.  * @ORM\Entity(repositoryClass="Acme\Bundle\AdminBundle\Entity\BrandDeviceRepository")
  14.  */
  15. class BrandDevice
  16. {
  17. /**
  18.   * @var string
  19.   *
  20.   * @ORM\Column(name="name", type="string", length=50, nullable=false)
  21.   * @Assert\NotBlank(message="Te pole jest wymagane")
  22.   */
  23. private $name;
  24.  
  25. /**
  26.   * @var string
  27.   *
  28.   * @ORM\Column(name="description", type="text", nullable=false)
  29.   */
  30. private $description;
  31.  
  32. /**
  33.   * @var string
  34.   *
  35.   * @ORM\Column(name="logo", type="string", length=50, nullable=false)
  36.   */
  37. private $logo;
  38.  
  39. /**
  40.   * @var string
  41.   *
  42.   * @ORM\Column(name="website", type="string", length=100, nullable=false)
  43.   * @Assert\NotBlank(message="Te pole jest wymagane")
  44.   * @Assert\Url(message="Proszę podać poprawny adres strony www")
  45.   */
  46. private $website;
  47.  
  48. /**
  49.   * @var integer
  50.   *
  51.   * @ORM\Column(name="id", type="integer")
  52.   * @ORM\Id
  53.   * @ORM\GeneratedValue(strategy="IDENTITY")
  54.   */
  55. private $id;
  56.  
  57.  
  58.  
  59. /**
  60.   * Set name
  61.   *
  62.   * @param string $name
  63.   * @return BrandDevice
  64.   */
  65. public function setName($name)
  66. {
  67. $this->name = $name;
  68.  
  69. return $this;
  70. }
  71.  
  72. /**
  73.   * Get name
  74.   *
  75.   * @return string
  76.   */
  77. public function getName()
  78. {
  79. return $this->name;
  80. }
  81.  
  82. /**
  83.   * Set description
  84.   *
  85.   * @param string $description
  86.   * @return BrandDevice
  87.   */
  88. public function setDescription($description)
  89. {
  90. $this->description = $description;
  91.  
  92. return $this;
  93. }
  94.  
  95. /**
  96.   * Get description
  97.   *
  98.   * @return string
  99.   */
  100. public function getDescription()
  101. {
  102. return $this->description;
  103. }
  104.  
  105. /**
  106.   * Set logo
  107.   *
  108.   * @param string $logo
  109.   * @return BrandDevice
  110.   */
  111. public function setLogo($logo)
  112. {
  113. $this->logo = $logo;
  114.  
  115. return $this;
  116. }
  117.  
  118. /**
  119.   * Get logo
  120.   *
  121.   * @return string
  122.   */
  123. public function getLogo()
  124. {
  125. return $this->logo;
  126. }
  127.  
  128. /**
  129.   * Set website
  130.   *
  131.   * @param string $website
  132.   * @return BrandDevice
  133.   */
  134. public function setWebsite($website)
  135. {
  136. $this->website = $website;
  137.  
  138. return $this;
  139. }
  140.  
  141. /**
  142.   * Get website
  143.   *
  144.   * @return string
  145.   */
  146. public function getWebsite()
  147. {
  148. return $this->website;
  149. }
  150.  
  151. /**
  152.   * Get id
  153.   *
  154.   * @return integer
  155.   */
  156. public function getId()
  157. {
  158. return $this->id;
  159. }
  160.  
  161.  
  162. }

BrandDeviceRepository:
  1. <?php
  2.  
  3. namespace Acme\Bundle\AdminBundle\Entity;
  4.  
  5. use Doctrine\ORM\EntityRepository;
  6.  
  7. /**
  8.  * BrandDeviceRepository
  9.  *
  10.  * This class was generated by the PhpStorm "Php Annotations" Plugin. Add your own custom
  11.  * repository methods below.
  12.  */
  13. class BrandDeviceRepository extends EntityRepository
  14. {
  15. public function findLikeName($term)
  16. {
  17. return $this->getEntityManager()
  18. ->createQuery(
  19. 'SELECT p FROM AcmeAdminBundle:BrandDevice p WHERE p.name LIKE :name'
  20. )
  21. ->setParameter('name','%'.$term.'%')
  22. ->setMaxResults(3)
  23. ->getResult();
  24. }
  25. }

DefaultController
  1. <?php
  2.  
  3. namespace Acme\Bundle\AdminBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10.  
  11. class DefaultController extends Controller
  12. {
  13. /**
  14.   * @Route("/autocomplete-test/search", name="autocomplete_test_search")
  15.   */
  16. public function autocompleteTestSearchAction(Request $request)
  17. {
  18. $q = $request->get('term');
  19. $repo = $this->getDoctrine()->getRepository('AcmeAdminBundle:BrandDevice');
  20. $brands = $repo->findLikeName($q);
  21. $results = array();
  22. foreach($brands as $brand)
  23. {
  24. $results[] = array('id'=>$brand->getId(),'name'=>$brand->getName(),'label'=>$brand->getName());
  25. }
  26. return new JsonResponse($results);
  27. }
  28. /**
  29.   * @Route("/autocomplete-test/{id}", name="autocomplete_test_get")
  30.   */
  31. public function autocompleteTestGetAction($id=1)
  32. {
  33. $em = $this->getDoctrine()->getRepository('AcmeAdminBundle:BrandDevice');
  34. $brand = $em->find($id);
  35.  
  36. return new Response($brand->getName());
  37. }
  38.  
  39. }


--
Jeśli zmienię typ pola city na "text" i usunę "class" to wtedy autouzupełnianie działa dobrze. Niestety gdy chcę przypisać klasę to otrzymuje w/w błąd.
Proszę o jakieś rady bo jestem ciekaw o co może chodzić...


Pozdrawiam
Go to the top of the page
+Quote Post
prz3kus
post 30.06.2015, 13:48:24
Post #2





Grupa: Zarejestrowani
Postów: 260
Pomógł: 30
Dołączył: 22.01.2007

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


Nigdy się nie bawiłem tym bundlem, ale może spróbuj dodać do klasy Entity

  1. public function __toString() {
  2. return $this->name;
  3. }

Może nie wie co wyświetlić smile.gif
Go to the top of the page
+Quote Post
aras785
post 30.06.2015, 15:15:12
Post #3





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

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


A bawiłeś się: https://github.com/genemu/GenemuFormBundle ?

Bo tutaj również mam problem...

Kontroler:
  1. /**
  2.   * @Route("/company/test", name="admin_company_test")
  3.   */
  4. public function test(Request $request)
  5. {
  6.  
  7.  
  8. $form = $this->createFormBuilder()
  9. ->add('name', 'text', array('label' => 'Nazwa'))
  10. ->add('choices', 'genemu_jqueryautocompleter_choice', array(
  11. 'choices' => array(
  12. 'value1' => 'Label1',
  13. 'value2' => 'Label2',
  14. 'value3' => 'Label3'
  15. )))
  16. ->add('save','submit')
  17. ->getForm();
  18.  
  19. $this->view_data['title']['subheader'] = 'Test';
  20. $this->view_data['form'] = $form->createView();
  21. return $this->render('@AcmeAdmin/Company/test.html.twig',$this->view_data);
  22.  
  23. }


i dostaję taki błąd:


Go to the top of the page
+Quote Post
Crozin
post 30.06.2015, 15:46:08
Post #4





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


1. Komunikat błędu jest niezwykle oczywisty. W którymś miejscu (masz podane przy komunikacie błędu) odwołujesz się do zmiennej jakby była obiektem, a ta nim nie jest (zapewne jest NULL-em). Zapewne, któraś z metod mająca zwrócić obiekt tego nie robi. Możesz podpiąć sobie debugger, możesz to zwykłym var_dumpem() sprawdzić.
2. Wklejaj tylko kawałki kodu mające jakiś związek z problemem i postaraj się ograniczyć jego ilość do niezbędnego minimum ukazującego problem: http://sscce.org/
Go to the top of the page
+Quote Post
prz3kus
post 1.07.2015, 07:15:22
Post #5





Grupa: Zarejestrowani
Postów: 260
Pomógł: 30
Dołączył: 22.01.2007

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


Prawde mówiąc kiedyś sprawdzałem kilka bundli z forami jednak nie przypadły mi do gustu i docelowo zawsze kończyułem na własnych tabelkach i ckeditorze, a jak ckeditor połączysz z uplodowaniem plików z sonaty to już masz wsyztko smile.gif

Nie wiem czy do tego doszło ale forum phpBB miał powstac jako bundle do symfony, tylko nie wiem czy to umarlo czy czekaja na php7 i symfony3 :]
Go to the top of the page
+Quote Post
aras785
post 3.07.2015, 07:52:57
Post #6





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

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


Dziękuje za odpowiedzi.

Mimo wszystko nie udało mi się ogarnąć tych bundli... w takim razie zrobię sobie to sam i od razu pytanie.

Mam sobie Entity np. PRODUCT i w nim:
ID,ID_CAT, NAME

I CATEGORY: ID, NAME

---
Jak teraz dodając produkt jak sprawdzić czy wpisany nr. kategorii (niech będzie pole "textowe" dla utrudnienia) jest prawidłowy czyli istnieje taka wartośc rzeczywiście w bazie "CATEGORY". Wiem, że tak działa pole typu entity ale chcę sam dodać sobie ajaxa do pola tekstowe i zaciągać dynamicznie wartości, a nie wszystko naraz jak to robi typ: entity.

Proszę o pomoc jak takie rzeczy rozwiązywać...

Pozdrawiam

Ten post edytował aras785 3.07.2015, 07:53:16
Go to the top of the page
+Quote Post
prz3kus
post 3.07.2015, 09:59:48
Post #7





Grupa: Zarejestrowani
Postów: 260
Pomógł: 30
Dołączył: 22.01.2007

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


Pole tekstowe nie przypiszesz, ponieważ klasa entity będzie żadać obiektu, jak nie możesz wyfiltrować wyboru na sztywno to zostaje Ci wyszukanie (zrobniebie selekta) na bazie i przypisanie zwróconego obiektu do tworzonego zapytania
Go to the top of the page
+Quote Post
aras785
post 3.07.2015, 10:17:41
Post #8





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

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


Cytat
...jak nie możesz wyfiltrować wyboru na sztywno to zostaje Ci wyszukanie (zrobniebie selekta) na bazie i przypisanie zwróconego obiektu do tworzonego zapytania.


Najprostszy przykład? Wiem, że truję du** ale próbuję to ogarnąć smile.gif

Dziękuje
Go to the top of the page
+Quote Post
ohm
post 3.07.2015, 10:32:24
Post #9





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

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


Cytat(aras785 @ 3.07.2015, 08:52:57 ) *
Jak teraz dodając produkt jak sprawdzić czy wpisany nr. kategorii (niech będzie pole "textowe" dla utrudnienia) jest prawidłowy czyli istnieje taka wartośc rzeczywiście w bazie "CATEGORY". Wiem, że tak działa pole typu entity ale chcę sam dodać sobie ajaxa do pola tekstowe i zaciągać dynamicznie wartości, a nie wszystko naraz jak to robi typ: entity.


Data transformer - http://symfony.com/doc/current/cookbook/fo...ansformers.html
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: 22.06.2025 - 14:21