2

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

  1. app/console generate:bundle --bundle-name=CompanyNameofBundle --format=annotation
  2. Bundle namespace: Company/nameofBundle
  3. Do you want to generate the whole directory structure: yes
  4. Do you confirm generation: return
  5. Confirm automatic update of your Kernel: yes
  6. Confirm automatic update of the Routing: yes

Create entities with crud

  1. 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
  2. app/console doctrine:mapping:import --force CompanyNameofBundle xml
  3. app/console doctrine:generate:entities CompanyNameofBundle
  4. 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!

shayster01
  • 214
  • 3
  • 17
  • Have you checked all namespaces and all the files' paths? Have you cleared the cache? –  Aug 07 '15 at 15:56
  • Yes I should have mentioned that I have also run: app/console cache:clear and app/console doctrine:cache:clear-metadata – shayster01 Aug 07 '15 at 16:05
  • You can also check you have not generate a yml or xml format before. – Sylvain Martin Aug 07 '15 at 16:06
  • Sylvain can you ellaborate because since I have done this several times before I want to make sure I checked everywhere? – shayster01 Aug 07 '15 at 16:08
  • You have to check in the Ressources/config/doctrine/metadata/orm/ folder that you have not file generated for your entity. I had this problem before because I made a mix of annotation and xml format. – Sylvain Martin Aug 07 '15 at 16:12
  • In my Resources/config/doctrine I only have the xml files generated when doing - app/console doctrine:mapping:import --force CompanyNameofBundle xml @SylvainMARTIN Which produced a Person.orm.xml – shayster01 Aug 07 '15 at 16:21
  • 1
    Ok it was just to check in case of xml format you have put – Sylvain Martin Aug 07 '15 at 16:27
  • You totally rock! So for clarification I manually edited my in Resources/config/doctrine/Person.orm.xml to look like this – shayster01 Aug 07 '15 at 16:33

1 Answers1

0

In a first time, you should have a look on the documentation to have a repository like this :

http://symfony.com/doc/current/book/doctrine.html

// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\ProductRepository")
 */
class Product
{
    //...
}

In a second time, you have to check in case of xml format you have put

<entity name="Acme\StoreBundle\Entity\Product" repository-class="Acme\StoreBundle\Entity\ProductRepository">

You cannot have a mix of annotation, xml or yml format to shape an entity in Symfony 2, remove all the unnecessary files.

Sylvain Martin
  • 2,365
  • 3
  • 14
  • 29