BentCoder
BentCoder

Reputation: 12730

Custom query in custom repo cannot be found

I just want to get the last record so I cannot call my custom query in custom build repo. Error is below. Do you know what it could be?

Note: Normal find(), findBy() like queries work fine. If there is a easier way of doing it then please tell me how to do it.

Thanks

ERROR:

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

TRYING TO USE WITH THIS:

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

Upvotes: 0

Views: 37

Answers (1)

FyodorX
FyodorX

Reputation: 1480

It seems like you're overwriting the Entity annotation. Try removing the second one:

namespace Booking\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

Upvotes: 2

Related Questions