Question

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
Was it helpful?

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

OTHER TIPS

You can also use the EntityRepository::findBy() method (outlined in the Symfony 2.x documentation here) which allows you to specify search criteria and sorting.

e.g.

$childCategories = $repository->findBy(array(
    'categoryParentId' => $pcategory->getCatagoryId()
  ),
  array(
    'categoryId' => 'ASC'
  )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top