1

I am trying to add a custom method to my repository. I need to execute a 'not equal to' query. So as this suggests i implemented the following.But i get an erroe like

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

In my entity

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

In my controller

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

In my orm.xml file

  <?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>

Also, when i try to execute the action involving findAll() method, it shows me undefined method error. How to fix this?

Community
  • 1
  • 1
Dency G B
  • 8,096
  • 9
  • 47
  • 78

1 Answers1

2

You must create that method inside your System\VmsBundle\Entity\EntryRepository file, not inside yourSystem\VmsBundle\Entity\EntryDetails file. It would look like this:

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

Also (I think that) you have an error in your orm.xml file. You need to change your repository-class on the 3ยบ line to repository-class="System\VmsBundle\Entity\EntryRepository"

Dani Sancas
  • 1,365
  • 11
  • 27