使用 QueryBuilder yii2 使用类似查询构建搜索的问题
Issue in building search with like query using QueryBuilder yii2
我正在尝试使用 yii2 QueryBuilder 对我的网站博客进行搜索查询,但是当我尝试使用 ->all()
执行查询时出现这样的错误。这是错误:strtr() expects parameter 1 to be string, object given
。这是我的模型和控制器。我不知道是什么导致了这个问题。
控制器:
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()
我得到了那个错误。
试试你的 ActiveQuery
作为:--
$result = (new Query())->select('id','title','blog','picture')
->from('blog')
->where(['like' , 'title' , $search])
->orWhere(['like' , 'blog' , $search])
->all();
echo '<pre>';
var_dump($result);
die();
您确定 $search 是字符串还是数组?
like: operand 1 should be a column or DB expression, and operand 2 be
a string or an array representing the values that the column or DB
expression should be like. For example, ['like', 'name', 'tester']
will generate name LIKE '%tester%'
.
When the value range is given as an array, multiple LIKE predicates
will be generated and concatenated using AND. For example, ['like',
'name', ['test', 'sample']]
will generate name LIKE '%test%' AND
name LIKE '%sample%'
. The method will properly quote the column name
and escape special characters in the values.
Sometimes, you may want to add the percentage characters to the
matching value by yourself, you may supply a third operand false
to
do so. For example, ['like', 'name', '%tester', false]
will generate
name LIKE '%tester'
.
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,并注意您正在使用的变量。
我正在尝试使用 yii2 QueryBuilder 对我的网站博客进行搜索查询,但是当我尝试使用 ->all()
执行查询时出现这样的错误。这是错误:strtr() expects parameter 1 to be string, object given
。这是我的模型和控制器。我不知道是什么导致了这个问题。
控制器:
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()
我得到了那个错误。
试试你的 ActiveQuery
作为:--
$result = (new Query())->select('id','title','blog','picture')
->from('blog')
->where(['like' , 'title' , $search])
->orWhere(['like' , 'blog' , $search])
->all();
echo '<pre>';
var_dump($result);
die();
您确定 $search 是字符串还是数组?
like: operand 1 should be a column or DB expression, and operand 2 be a string or an array representing the values that the column or DB expression should be like. For example,
['like', 'name', 'tester']
will generatename LIKE '%tester%'
.When the value range is given as an array, multiple LIKE predicates will be generated and concatenated using AND. For example,
['like', 'name', ['test', 'sample']]
will generatename LIKE '%test%' AND name LIKE '%sample%'
. The method will properly quote the column name and escape special characters in the values.Sometimes, you may want to add the percentage characters to the matching value by yourself, you may supply a third operand
false
to do so. For example,['like', 'name', '%tester', false]
will generate nameLIKE '%tester'
.
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,并注意您正在使用的变量。