无法找到自定义仓库中的自定义查询

时间:2014-09-04 21:18:43

标签: symfony doctrine-orm

我只想获取最后一条记录,因此无法在自定义构建存储库中调用自定义查询。错误如下。你知道它会是什么吗?

注意:正常的find(),findBy()之类的查询工作正常。如果有更简单的方法,请告诉我该怎么做。

由于

错误:

 Undefined method 'getLastRecord'. The method name must start with either findBy or findOneBy!

MAIN CLASS:

namespace Booking\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="CashQuery")
 * @ORM\Entity(repositoryClass="Booking\AdminBundle\Entity\CashQueryRepository")
 * @ORM\Entity
 */
class CashQuery
{
}

REPO:

namespace Booking\AdminBundle\Entity;

use Doctrine\ORM\EntityRepository;

class CashQueryRepository extends EntityRepository
{
    public function getLastRecord()
    {
        return $this->getEntityManager()
            ->createQuery(
                'SELECT d FROM AdminBundle:CashQuery d ORDER BY d.id DESC LIMIT 1'
            )
            ->getResult();
    }
}

尝试使用此功能:

$dcq = $em->getRepository('BookingAdminBundle:CashQuery')->getLastRecord();
if (! $dcq) {
    echo 'CashQuery could not found';
    return false;
}
echo $dcq->getCreatedAt();

1 个答案:

答案 0 :(得分:2)

好像你正在覆盖Entity注释。尝试删除第二个:

namespace Booking\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="CashQuery")
 * @ORM\Entity(repositoryClass="Booking\AdminBundle\Entity\CashQueryRepository")
 */
class CashQuery
{
}