我正在尝试向我的存储库添加自定义方法。我需要执行'不等于'的查询。因此,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()方法的操作时,它会显示未定义的方法错误。如何解决这个问题?
答案 0 :(得分:2)
您必须在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"