我正在尝试使用 yii2 QueryBuilder 对我的网站博客进行搜索查询,但是当我尝试使用 ->all()
执行查询时出现这样的错误。这是错误:strtr() expects parameter 1 to be string, object given
。这是我的模型和 Controller 。我不知道是什么导致了这个问题。
Controller :
public function actionSearchBlog()
{
$model = new Blog();
if ($model->load(Yii::$app->request->post())) {
Blog::searchBlog($model->search);
} else {
return $this->render('search',['model' => $model]);
}
}
型号:
public static function searchBlog($search = null)
{
$search = new Query();
$result = $search->select('id','title','blog','picture')
->from('blog')
->where(['like' , 'title' , $search])
->orWhere(['like' , 'blog' , $search])
->all();
echo '<pre>';
var_dump($result);
die();
}
我尝试了最后没有 ->all()
的查询,但是 var_dump
值将是查询本身,它不会被执行。使用 ->all()
我得到了那个错误。
最佳答案
public static function searchBlog($search = null)
{
$query = new Query();
$result = $query->select('id','title','blog','picture')
->from('blog')
->where(['like' , 'title' , $search])
->orWhere(['like' , 'blog' , $search])
->all();
echo '<pre>';
var_dump($result);
die();
}
这会起作用。但是开始使用 IDE,并注意您正在使用的变量。
关于php - 使用 QueryBuilder yii2 使用类似查询构建搜索的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35031467/