6
votes

I have the class user repository

class userRepository extends EntityRepository
{

    function getuserData($id)
    {
        $query = $this->createQuery('
                SELECT c FROM AcmeBundle:User c 
                WHERE c.id = :id ORDER BY c.id ASC
            ')
            ->setParameter('id', $id);

        return $query->getResult();
    }
}

I am getting this error

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

4

4 Answers

9
votes

According to docs

$em = $this->getEntityManager();
$query = $em->createQuery('
        SELECT p FROM AcmeStoreBundle:Product p 
        WHERE p.price > :price 
        ORDER BY p.price ASC
    ')
    ->setParameter('price', '19.99');

$products = $query->getResult();
2
votes

I know this answer is quite late but your userRepository class needs to be defined in your User entity.

use Doctrine\ORM\Mapping as ORM
/**
 * @ORM\Entity(repositoryClass="NameOfBundle\Repository\UserRepository")
class User
{
    ...
}
1
votes

When we work at Repositories

$em = $this->getEntityManager(); //wont work in repository
$em->createQueryBuilder($sql); 

you have to use

$conn = $this->getEntityManager()->getConnection();     
$stmt = $conn->prepare($sql);

Read More

0
votes

The method CreateQuery is of Entity Manager, is not Controller's class