Ich habe eine Tabelle, die ein Feld `activated_at` timestamp NULL DEFAULT NULL
hat, was bedeutet, dass es einen Zeitstempel enthalten kann oder es kann null
sein und es ist null
standardmäßig.Wie IS NULL und IS NOT NULL mit Yii 2 ActiveRecord tun?
Ich habe eine andere [gii generierte] Suche Modell mit einer folgenden Konfiguration in der search()
Methode:
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;
}
Ja, ich habe die activated
Eigenschaft in meiner Klasse gesetzt:
public $activated;
Und Meine rules()
Methode ist wie folgt:
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'],
];
}
Was ich in der search()
Methode zu setzen versuchte, auf dem Feld activated_at
je nach $activated
Wert zu filtern ist (oben Code):
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;
}
}
ich es mit GridView
verwenden - alle anderen Filterwerke außer dieser.
Was mache ich hier falsch?
Aand wie richtig diese Art von Abfragen zu tun:
IS NULL something
IS NOT NULL something
Mit ActiveRecord
Query Builder des Yü 2?
EDIT: Line: if(!isset($_GET['deleted']))
für etwas anderes verwendet wird, und das funktioniert normal.
Ja, ich habe das gerade gelesen und habe meine Antwort direkt nach Ihrer gepostet. Ich werde deine trotzdem akzeptieren. Vielen Dank! – omerowitz