pagekit - 查询搜索问题

时间:2017-07-30 09:36:19

标签: php symfony pagekit

我创建了一个名为material的扩展程序,一切正常。我想实现简单的搜索查询,因此我从bixie/portfolio扩展程序对其进行了采样,您可以从marketplace获取它。所以我在MaterialController

中有这段代码
$user_id = App::user()->id;
        $query  = array_values(Material::query()->where('user_id = ?', [$user_id])->get());
        $filter = array_merge(array_fill_keys(['search', 'order', 'limit'], ''), $filter);

        extract($filter, EXTR_SKIP);

        if ($search) {
            $query->where(function ($query) use ($search) {  //<-- this throw error.
                $query->orWhere(['name LIKE :search'], ['search' => "%{$search}%"]);
            });
        }

我会得到致命的错误:

  

致命错误:调用成员函数where()on array on line ...

错误是什么意思?

我用google搜索了一些评论函数where()需要与集合一起使用。在这种情况下我该如何解决?

我正在使用pagekit 1.0.12symfony ~3.0.0

1 个答案:

答案 0 :(得分:0)

正确版本的代码是:

$user_id = App::user()->id;
// init query and add filter by `user_id`
// DO NOT use `get` and `array_values`, because 
// these functions __change__ type of `$query`
$query  = Material::query()->where('user_id = ?', [$user_id]);

$filter = array_merge(array_fill_keys(['search', 'order', 'limit'], ''), $filter);
extract($filter, EXTR_SKIP);

if ($search) {
    // add additional filtering
    $query->where(function ($query) use ($search) {
        $query->orWhere(['name LIKE :search'], ['search' => "%{$search}%"]);
    });
}

// get/return results of a query
return $query->get();