我想用yii2搜索模型创建这个查询
select * from t1 where (title = 'keyword' or content = 'keyword') AND
(category_id = 10 or term_id = 10 )
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用orFilterWhere和andFilterWhere.
我在搜索模型中的代码:
public function search($params) {
$query = App::find();
//...
if ($this->keyword) {
$query->orFilterWhere(['like', 'keyword', $this->keyword])
->orFilterWhere(['like', 'content', $this->keyword])
}
if ($this->cat) {
$query->orFilterWhere(['category_id'=> $this->cat])
->orFilterWhere(['term_id'=> $this->cat])
}
//...
}
Run Code Online (Sandbox Code Playgroud)
但它创建了这个查询:
select * from t1 where title = 'keyword' or content = 'keyword' or
category_id = 10 or term_id = 10
Run Code Online (Sandbox Code Playgroud) 我想用一个表添加记录ActiveRecord.
这是我的代码:
$model = new Article();
$model->title = 'test title';
$model->content = 'test content';
$model->category_id = 1;
$model->author_id = 1;
if ($model->validate()) {
$model->save();
}
Run Code Online (Sandbox Code Playgroud)
$model->validate()返回true,但$model->save()返回false.
如何查找生成的原始sql $model->save()?
与此同时:
$model->save()->rawSql是null并$model->getErrors()返回空数组.
在调试中,将记录所有查询,但我没有找到任何插入或更新查询.
我有一个包含相关数据的实体,我想将其保存在新记录中。
我试试:
$newTour = $this->Tours->get($id, ['contain' => ['Cities', 'Tags']]);
$newTour->set('id', null);
$this->Tours->save($newTour);
Run Code Online (Sandbox Code Playgroud)
但我看到了这个错误:
更新需要所有主键值
我该怎么办?