Search code examples
phpsymfonydoctrine-ormdoctrinesymfony1

Doctrine select where equal to variable


This is what I want to do:

$query = $repository->createQueryBuilder('c')
     ->where('c.categoryParentid = ?', $pcategory->getcategoryId())
     ->orderBy('c.categoryId', 'ASC')
     ->getQuery();

$childcategories = $query->getResult();

The $pcategory->getcategoryId() is an integer. The where, is this correct? I get this error:

ContextErrorException: Warning: get_class() expects parameter 1 to be object, integer given in /Applications/mampstack-5.4.20-0/apache2/htdocs/reuzze/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Expr/Base.php line 92

Solution

  • I haven't used that syntax before, but the Symfony docs have examples of using the query builder, and they show something like this:

    $query = $repository->createQueryBuilder('c')
     ->where('c.categoryParentid = :pcategory')
     ->setParameter('pcategory', $pcategory->getcategoryId())
     ->orderBy('c.categoryId', 'ASC')
     ->getQuery();