문제

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
도움이 되었습니까?

해결책

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

다른 팁

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'
  )
);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top