繁体   English   中英

Symfony 2错误:在数组上调用成员函数andWhere()

[英]Symfony 2 Error: Call to a member function andWhere() on array

我正在尝试执行QueryBuilder查询,但它不喜欢for循环中的“ andWhere”语法。 我认为它不喜欢for循环中的任何东西。 应该检查是否有任何数组元素等于“ x”,以便根据该元素过滤搜索结果。

$repository = $this->getDoctrine()->getRepository('AppBundle:Shrubs');

$query = $repository->createQueryBuilder('p');

$shrubs = $query
    ->where($query->expr()->like('p.botanicalname', ':botanicalname'))
    ->setParameter('botanicalname', '%' . $botanicalname . '%')
    ->andwhere($query->expr()->like('p.commonname', ':commonname'))
    ->setParameter('commonname', '%' . $commonname . '%')
    ->orderBy('p.commonname', 'ASC')
    ->getQuery()
    ->getResult();

$checkfor = array("wetsoil"=>"Tolerates Wet Soil",
    "borderlinehardy"=>"Borderline Hardy",
    "moistsoil"=>"Prefers Moist Soil";

reset($checkfor);

foreach ($checkfor as $key => $value) {
    if (${$key} == "x") {
        $shrubs = $shrubs->andWhere('$key = x')
            ->setParameter('x', $key)
            ->getQuery()
            ->getResult();
        return $this->render('shrubs/searchresults.html.twig', array(
            'shrubs' => $shrubs,));
    }
}

return $this->render('shrubs/searchresults.html.twig', array(
    'shrubs' => $shrubs

您的$shrubs变量是$query结果,您调用->getQuery()->getResult()

您固定的代码看起来像

$repository = $this->getDoctrine()->getRepository('AppBundle:Shrubs');

$query = $repository->createQueryBuilder('p');

$query
    ->where($query->expr()->like('p.botanicalname', ':botanicalname'))
    ->setParameter('botanicalname', '%' . $botanicalname . '%')
    ->andwhere($query->expr()->like('p.commonname', ':commonname'))
    ->setParameter('commonname', '%' . $commonname . '%')
    ->orderBy('p.commonname', 'ASC')
    ; // Remove ->getQuery() and->getResult()

$checkfor = array("wetsoil"=>"Tolerates Wet Soil",
    "borderlinehardy"=>"Borderline Hardy",
    "moistsoil"=>"Prefers Moist Soil";

reset($checkfor);

foreach ($checkfor as $key => $value) {
    if (${$key} == "x") {
        $shrubs = $query->andWhere('$key = x')
            ->setParameter('x', $key)
            ->getQuery()
            ->getResult();
        return $this->render('shrubs/searchresults.html.twig', array(
            'shrubs' => $shrubs,));
    }
}

$shrubs = $query->getQuery()->getResult();// Execute the query here

return $this->render('shrubs/searchresults.html.twig', array(
    'shrubs' => $shrubs
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM