【问题标题】:Add a custom method to my repository gives me error向我的存储库添加自定义方法给我错误
【发布时间】:2014-02-12 01:26:32
【问题描述】:

我正在尝试将自定义方法添加到我的存储库。我需要执行一个“不等于”查询。所以this 建议我实现了以下内容。但我得到了一个类似的错误

 FatalErrorException: Error: Call to undefined method System\VmsBundle\Entity\EntryDetails::createQueryBuilder() in ..\src\System\VmsBundle\Entity\EntryDetails.php line 208

在我的实体中

 <?php

 namespace System\VmsBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
 /**
 *@ORM\Entity(repositoryClass="System\VmsBundle\Entity\EntryRepository")
 **/

 /**
  * EntryDetails
  */
 class EntryDetails
 {
 .....
     public function findByNot($field, $value)
     {

     $qb = $this->createQueryBuilder('a');
     $qb->where($qb->expr()->not($qb->expr()->eq('a.'.$field, '?1')));
     $qb->setParameter(1, $value);
     return $qb->getQuery()->getResult();
 }
 }

在我的控制器中

 $entities = $this->getDoctrine()->getRepository('SystemVmsBundle:EntryDetails')->findByNot('exitTime', 1);

在我的 orm.xml 文件中

  <?xml version="1.0" encoding="utf-8"?>
  <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="System\VmsBundle\Entity\EntryDetails" table="entry_details" repository-class="System\VmsBundle\Entity\EntryDetails">
  ....
  </entity>
  </doctrine-mapping>

另外,当我尝试执行涉及 findAll() 方法的操作时,它会显示未定义的方法错误。如何解决这个问题?

【问题讨论】:

    标签: symfony


    【解决方案1】:

    您必须在System\VmsBundle\Entity\EntryRepository 文件中创建该方法,而不是在System\VmsBundle\Entity\EntryDetails 文件中。它看起来像这样:

    <?php
    
    namespace System\VmsBundle\Entity;
    
    use Doctrine\ORM\EntityRepository;
    
    class EntryRepository extends EntityRepository
    {
        public function findByNot($field, $value)
         {
             $qb = $this->createQueryBuilder('a');
             $qb->where($qb->expr()->not($qb->expr()->eq('a.'.$field, '?1')));
             $qb->setParameter(1, $value);
             return $qb->getQuery()->getResult();
         }
    }
    

    另外(我认为)您的 orm.xml 文件中有错误。您需要将 3º 线上的 repository-class 更改为 repository-class="System\VmsBundle\Entity\EntryRepository"

    【讨论】:

    • 工作完美!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2017-12-03
    • 1970-01-01
    相关资源
    最近更新 更多