ome*_*itz 14 php activerecord yii yii2
我有其中有一个字段的表`activated_at` timestamp NULL DEFAULT NULL
,这意味着它可以包含时间戳,也可以是null
和它的null
默认.
我有另一个[gii-generated]搜索模型,在该search()
方法中具有以下配置:
public function search($params)
{
$query = User::find();
// add conditions that should always apply here
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$andFilterWhere = [
'id' => $this->id,
'status' => $this->status,
'role' => $this->role,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'completed_files' => $this->completed_files,
// 'activated_at' => null,
];
if(!isset($_GET['deleted'])) {
$query->where(['deleted_at' => null]);
$andFilterWhere['deleted_at'] = null;
} else if($_GET['deleted'] === 'true') {
$query->where(['not', ['deleted_at' => null]]);
}
// grid filtering conditions
$query->andFilterWhere(
$andFilterWhere
);
$query->andFilterWhere(['like', 'first_name', $this->username])
->andFilterWhere(['like', 'auth_key', $this->auth_key])
->andFilterWhere(['like', 'password_hash', $this->password_hash])
->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'first_name', $this->first_name])
->andFilterWhere(['like', 'last_name', $this->last_name]);
if($this->activated || $this->activated === "0") {
#die(var_dump($this->activated));
if($this->activated === '1') {
// this doesn't filter
$query->andFilterWhere(['not', ['activated_at' => null]]);
} else if($this->activated === '0') {
// this doesn't either
$query->andFilterWhere(['activated_at', null]);
}
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
}
Run Code Online (Sandbox Code Playgroud)
是的,我activated
在我班上设置了这个属性:
public $activated;
Run Code Online (Sandbox Code Playgroud)
我的rules()
方法如下:
public function rules()
{
return [
[['id', 'status', 'role', 'created_at', 'updated_at', 'completed_files'], 'integer'],
['activated', 'string'],
[['username', 'first_name', 'last_name', 'auth_key', 'password_hash', 'password_reset_token', 'email', 'deleted_at', 'completed_files', 'activated_at'], 'safe'],
];
}
Run Code Online (Sandbox Code Playgroud)
我试图在search()
方法中设置的是activated_at
根据$activated
值过滤字段(参见上面的代码):
if($this->activated || $this->activated === "0") {
#die(var_dump($this->activated));
if($this->activated === '1') {
// this doesn't filter
$query->andFilterWhere(['not', ['activated_at' => null]]);
} else if($this->activated === '0') {
// this doesn't either
$query->andFilterWhere(['activated_at', null]);
$andFilterWhere['activated_at'] = null;
}
}
Run Code Online (Sandbox Code Playgroud)
我用它GridView
- 除了这个之外,其他每个过滤器都有效.
我在这做错了什么?
Aand如何正确地执行此类查询:
IS NULL something
IS NOT NULL something
Run Code Online (Sandbox Code Playgroud)
使用Yii 2的ActiveRecord
查询构建器?
编辑:行:if(!isset($_GET['deleted']))
用于其他东西,这是正常的.
sca*_*dge 21
如果我理解正确,你可以使用和在哪里
->andWhere(['not', ['activated_at' => null]])
Run Code Online (Sandbox Code Playgroud)
但是,在执行的地方,相关值不为空
来自doc http://www.yiiframework.com/doc-2.0/yii-db-query.html
andFilterWhere()向现有条件添加一个额外的WHERE条件,但忽略空操作数.
小智 13
对于这个表达式:
WHERE activated_at IS NULL
Run Code Online (Sandbox Code Playgroud)
尝试这个(它的工作):
->andWhere(['is', 'activated_at', new \yii\db\Expression('null')]),
Run Code Online (Sandbox Code Playgroud)
小智 9
$null = new Expression('NULL');
$query->andFilterWhere(['is not', 'asp_id', $null]);
Run Code Online (Sandbox Code Playgroud)
或者
$query->andFilterWhere(['is', 'asp_id', $null]);
Run Code Online (Sandbox Code Playgroud)