【问题标题】:Doctrine select where equal to variable学说选择等于变量的地方
【发布时间】:2014-01-03 22:13:56
【问题描述】:

这就是我想做的:

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

$childcategories = $query->getResult();

$pcategory->getcategoryId() 是一个整数。在哪里,这是正确的吗? 我收到此错误:

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

【问题讨论】:

    标签: php symfony doctrine-orm doctrine symfony1


    【解决方案1】:

    我以前没有使用过这种语法,但 Symfony 文档中有 using the query builder 的示例,它们显示如下:

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

    【讨论】:

    • 模型上的属性应该是 categoryParent,并且持有一个对象,而不是一个 id。
    【解决方案2】:

    您还可以使用EntityRepository::findBy() 方法(在 Symfony 2.x 文档here 中进行了概述),它允许您指定搜索条件和排序。

    例如

    $childCategories = $repository->findBy(array(
        'categoryParentId' => $pcategory->getCatagoryId()
      ),
      array(
        'categoryId' => 'ASC'
      )
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-04
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2021-07-14
      相关资源
      最近更新 更多