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:
/** * @Route("/company/create", name="admin_company_create") */ public function create(Request $request) { $company = new Company(); $form = $this->createFormBuilder($company) ->getForm(); $form->handleRequest($request); if($form->isValid()) { $company->setDateAdd(new \DateTime()); $company->setDateUpdate(new \DateTime()); $em = $this->getDoctrine()->getManager(); $em->persist($company); $em->flush(); $this->addFlash('success','Dodano firmę'); } //title $this->view_data['title']['subheader'] = 'Dodaj'; $this->view_data['form'] = $form->createView(); return $this->render('AcmeAdminBundle:Company:create.html.twig', $this->view_data); }
BrandDevice:
<?php namespace Acme\Bundle\AdminBundle\Entity; use Cocur\Slugify\Slugify; use Doctrine\ORM\Mapping as ORM; /** * BrandDevice * * @ORM\Table(name="brand_device") * @ORM\Entity(repositoryClass="Acme\Bundle\AdminBundle\Entity\BrandDeviceRepository") */ class BrandDevice { /** * @var string * * @ORM\Column(name="name", type="string", length=50, nullable=false) * @Assert\NotBlank(message="Te pole jest wymagane") */ private $name; /** * @var string * * @ORM\Column(name="description", type="text", nullable=false) */ private $description; /** * @var string * * @ORM\Column(name="logo", type="string", length=50, nullable=false) */ private $logo; /** * @var string * * @ORM\Column(name="website", type="string", length=100, nullable=false) * @Assert\NotBlank(message="Te pole jest wymagane") * @Assert\Url(message="Proszę podać poprawny adres strony www") */ private $website; /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * Set name * * @param string $name * @return BrandDevice */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set description * * @param string $description * @return BrandDevice */ public function setDescription($description) { $this->description = $description; return $this; } /** * Get description * * @return string */ public function getDescription() { return $this->description; } /** * Set logo * * @param string $logo * @return BrandDevice */ public function setLogo($logo) { $this->logo = $logo; return $this; } /** * Get logo * * @return string */ public function getLogo() { return $this->logo; } /** * Set website * * @param string $website * @return BrandDevice */ public function setWebsite($website) { $this->website = $website; return $this; } /** * Get website * * @return string */ public function getWebsite() { return $this->website; } /** * Get id * * @return integer */ public function getId() { return $this->id; } }
BrandDeviceRepository:
<?php namespace Acme\Bundle\AdminBundle\Entity; use Doctrine\ORM\EntityRepository; /** * BrandDeviceRepository * * This class was generated by the PhpStorm "Php Annotations" Plugin. Add your own custom * repository methods below. */ class BrandDeviceRepository extends EntityRepository { public function findLikeName($term) { return $this->getEntityManager() ->createQuery( 'SELECT p FROM AcmeAdminBundle:BrandDevice p WHERE p.name LIKE :name' ) ->setParameter('name','%'.$term.'%') ->setMaxResults(3) ->getResult(); } }
DefaultController
<?php namespace Acme\Bundle\AdminBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class DefaultController extends Controller { /** * @Route("/autocomplete-test/search", name="autocomplete_test_search") */ public function autocompleteTestSearchAction(Request $request) { $q = $request->get('term'); $repo = $this->getDoctrine()->getRepository('AcmeAdminBundle:BrandDevice'); $brands = $repo->findLikeName($q); foreach($brands as $brand) { } return new JsonResponse($results); } /** * @Route("/autocomplete-test/{id}", name="autocomplete_test_get") */ public function autocompleteTestGetAction($id=1) { $em = $this->getDoctrine()->getRepository('AcmeAdminBundle:BrandDevice'); $brand = $em->find($id); return new Response($brand->getName()); } }
--
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