Symfony2未定义的方法'findLikeFullnameArray'。方法名称必须以findBy或findOneBy

时间:2015-08-07 15:53:01

标签: php symfony doctrine-orm

我已经阅读了有关此问题的各种解决方案,但似乎都没有效果。我真的想知道这个问题的核心是什么。我将准确列出我所做的事情,因为它相对简单,我无法理解我所缺少的东西。

所以我创建了一个带有表人的简单数据库,我正在尝试使用bootstrap生成CRUD,我工作正常。我的问题是当我尝试使用jquery插件来处理自动完成时。接下来我添加一个存储库来处理我的查询,当我得到Symfony2 Undefined方法'findLikeFullnameArray'消息时。我试图只使用注释,所以如果我的过程中出现问题,请告诉我。

以下是我的命令: 创建捆绑包

  1. app / console generate:bundle --bundle-name = CompanyNameofBundle --format = annotation
  2. Bundle namespace:Company / nameofBundle
  3. 是否要生成整个目录结构:是
  4. 你确认代:返回
  5. 确认内核的自动更新:是
  6. 确认自动更新路由:是
  7. 使用crud创建实体

    1. app / console generate:bundle --bundle-name = CompanyNameofBundle --format = annotation     Bundle namespace:Company / nameofBundle     你想生成整个目录结构:是的     你确认一代:回归     确认内核的自动更新:是的     确认自动更新路由:是
    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
    5. 然后我创建了我的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);
          }
      }
      

      人员实体:

      <?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;
          }
      }
      

      最后是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();
          }
      }
      

      以防这是我的app / config / routing.yml

      company_nameofbundle:
          resource: "@CompanyNameofBundle/Controller/"
          type:     annotation
          prefix:   /
      

      提前致谢!

1 个答案:

答案 0 :(得分:0)

第一次,您应该查看文档以获得这样的存储库:

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

AudioRecord

第二次,您必须检查已放置的// src/AppBundle/Entity/Product.php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="AppBundle\Entity\ProductRepository") */ class Product { //... } 格式

xml

您不能混合使用<entity name="Acme\StoreBundle\Entity\Product" repository-class="Acme\StoreBundle\Entity\ProductRepository">annotationxml格式来塑造Symfony 2中的实体,删除所有不必要的文件。