1
votes

Hi i am getting this error while i am calling a custom function defined inside my repository class in Symfony

Code In Repo Class

class TaskRepository extends DocumentRepository
{

public function searchByFirstLetter($letter)
{

    $query = $this->getDocumentManager()            
    ->createQuery('SELECT u FROM WorkStackTaskBundle:Users u WHERE u.name LIKE :name')
    ->setParameter('name', $letter.'%');
    try {
    return $query->getResult();
    } catch (\Doctrine\ODM\NoResultException $e) {
    return null;
    }}

Code In Controller $getasllusers = dm->getRepository('WorkStackTaskBundle:Users')->searchByFirstLetter($_GET['keyword']);

But i am getting following error.

Undefined method 'searchByFirstLetter'. The method name must start with either findBy or findOneBy! strong text Please suggest what might be wrong.

Thanks Advance

1
check this similar question, maybe you can find some solutions.Leo Silence

1 Answers

1
votes

You should define your custom repository class in two locations

/**
 * @MongoDB\Document(repositoryClass="Work\StackTaskBundle\Repository\UserRepository")
 */
class Users

And in

# src/Work/StackTaskBundle/Resources/config/services.yml
_repository:
    class: Work\StackTaskBundle\Repository\UserRepository
    factory_service: doctrine_mongodb.odm.document_manager
    factory_method:  getRepository
    arguments: ["Work\StackTaskBundle\Document\Users"]