小编ing*_*ous的帖子

Yii2:如何使用orWhere in和Where

我想用yii2搜索模型创建这个查询

select * from t1 where (title = 'keyword' or content = 'keyword') AND 
                       (category_id = 10 or term_id = 10 )
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用orFilterWhereandFilterWhere.

我在搜索模型中的代码:

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)

yii yii2

23
推荐指数
1
解决办法
4万
查看次数

Yii2:获取model-> save()的原始sql

我想用一个表添加记录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()->rawSqlnull$model->getErrors()返回空数组.

在调试中,将记录所有查询,但我没有找到任何插入或更新查询.

php activerecord yii yii2

6
推荐指数
1
解决办法
7150
查看次数

cakephp 3 如何从现有实体创建新记录

我有一个包含相关数据的实体,我想将其保存在新记录中。

我试试:

$newTour = $this->Tours->get($id, ['contain' => ['Cities', 'Tags']]);
$newTour->set('id', null);
$this->Tours->save($newTour);
Run Code Online (Sandbox Code Playgroud)

但我看到了这个错误:

更新需要所有主键值

我该怎么办?

cakephp cakephp-3.0

2
推荐指数
1
解决办法
1938
查看次数

标签 统计

yii ×2

yii2 ×2

activerecord ×1

cakephp ×1

cakephp-3.0 ×1

php ×1