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

时间:2017-03-29 12:32:01

标签: php arrays symfony for-loop query-builder

我正在尝试执行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

1 个答案:

答案 0 :(得分:1)

您的<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.myapplication70.MainActivity"> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="Result" android:inputType="numberDecimal" tools:layout_editor_absoluteX="67dp" tools:layout_editor_absoluteY="223dp" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="number" tools:layout_editor_absoluteX="67dp" tools:layout_editor_absoluteY="60dp" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="numberSigned" tools:layout_editor_absoluteX="67dp" tools:layout_editor_absoluteY="134dp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" tools:layout_editor_absoluteX="100dp" tools:layout_editor_absoluteY="319dp" /> </android.support.constraint.ConstraintLayout> 变量是$shrubs结果,您致电$query

您修复的代码看起来像

->getQuery()->getResult()