I have read through various solutions for this issue but none seem to work. I really want to know what is at the heart of this issue. I am going to list exactly what I did since it is relatively simple and I can't understand what I am missing.
So I have created a simple database with a table person and I am trying to generate CRUD with bootstrap which I get working fine. My issue is when I try to get jquery plugin to work with autocomplete. Next thing I add a repository to handle my query and that's when I get the Symfony2 Undefined method 'findLikeFullnameArray' message. I am trying to use just annotations so if there is something wrong in my process please let me know.
Here are my commands: Create Bundle
- app/console generate:bundle --bundle-name=CompanyNameofBundle --format=annotation
- Bundle namespace: Company/nameofBundle
- Do you want to generate the whole directory structure: yes
- Do you confirm generation: return
- Confirm automatic update of your Kernel: yes
- Confirm automatic update of the Routing: yes
Create entities with crud
- app/console generate:bundle --bundle-name=CompanyNameofBundle --format=annotation Bundle namespace: Company/nameofBundle Do you want to generate the whole directory structure: yes Do you confirm generation: return Confirm automatic update of your Kernel: yes Confirm automatic update of the Routing: yes
- app/console doctrine:mapping:import --force CompanyNameofBundle xml
- app/console doctrine:generate:entities CompanyNameofBundle
- app/console generate:doctrine:crud –entity=CompanyNameofBundle:Entityname --format=annotation --with-write –no-interaction
I then create my SearchController:
namespace Company\NameofBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Company\NameofBundle\Form\JqueryType;
use Company\NameofBundle\Form\SearchType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Company\NameofBundle\Entity\Person;
/**
* Search controller.
*
* @Route("/search")
*/
class SearchController extends Controller
{
/**
* @Route("/", name="search")
* @Template()
*/
public function indexAction()
{
$form = $this->createForm(new SearchType(), null, [
'action' => '',
'method' => 'POST'
]);
return array(
'form' => $form->createView(),
);
}
/**
* @Route("/person_search", name="person_search")
* @Template()
*
* @param Request $request
*
* @return array
*/
public function searchPersonAction(Request $request)
{
$q = $request->get('term');
$em = $this->getDoctrine()->getManager();
$results = $em->getRepository('CompanyNameofBundle:Person')->findLikeFullname($q);
return array('results' => $results);
}
/**
* @Route("/person_get", name="person_get")
*
* @param $id
*
* @return Response
*/
public function getPersonAction($id)
{
$em = $this->getDoctrine()->getManager();
$book = $em->getRepository('CompanyNameofBundle:Person')->find($id);
return new Response($book->getFullname());
}
/**
* @Route("/jquery", name="jquery")
* @Template()
*/
public function jqueryAction()
{
$form = $this->createForm(new JqueryType(), null, [
'action' => '',
'method' => 'POST'
]);
return array(
'form' => $form->createView(),
);
}
/**
* @Route("/jquery_search/{phrase}", name="jquery_search")
*
* @param string $phrase
*
* @return JsonResponse
*/
public function searchJqueryAction($phrase)
{
$em = $this->getDoctrine()->getManager();
$results = $em->getRepository('CompanyNameofBundle:Person')->findLikeFullnameArray($phrase);
return new JsonResponse($results);
}
}
Person Entity:
<?php
namespace Company\NameofBundle\Person;
use Doctrine\ORM\Mapping as ORM;
/**
* Person
* @ORM\Table()
* @ORM\Entity(repositoryClass="Company\NameofBundle\Entity\Repository\PersonRepository")
*/
class Person
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="fullname", type="string", length=255)
*/
private $fullname;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set fullname
*
* @param string $fullname
* @return Person
*/
public function setFullname($fullname)
{
$this->fullname = $fullname;
return $this;
}
/**
* Get fullname
*
* @return string
*/
public function getFullname()
{
return $this->fullname;
}
/**
* Set email
*
* @param string $email
* @return Person
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
}
Lastly PersonRepository
<?php
namespace Company\NameofBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
class PersonRepository extends EntityRepository
{
public function findLikeFullnameArray($fullname)
{
return $this->createQueryBuilder('person_repository')
->where('person_repository.fullname LIKE :name')
->setParameter('name', '%' . $fullname . '%')
->getQuery()
->getArrayResult();
}
}
In just in case here is my app/config/routing.yml
company_nameofbundle:
resource: "@CompanyNameofBundle/Controller/"
type: annotation
prefix: /
Thanks in advance!