Wie kann man das Vorhandensein eines Modells in der DB überprüfen? In Yü 1-Version war es so User::model()->exist()
Yii2: check exist ActiveRecord Modell in der Datenbank
18
A
Antwort
39
In Yii2 Sie exists()
auf Ihre Anfrage Kette hinzufügen:
User::find()
->where([ 'id' => 1 ])
->exists();
(Die generierten SQL wie folgt aussieht:. SELECT 1 FROM `tbl_user` WHERE `id`=1
)
Hier ist auch Query->exists()
von the Yii2 source.
/**
* Returns a value indicating whether the query result contains any row of data.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return boolean whether the query result contains any row of data.
*/
public function exists($db = null)
{
$select = $this->select;
$this->select = [new Expression('1')];
$command = $this->createCommand($db);
$this->select = $select;
return $command->queryScalar() !== false;
}
0
if(Mastersettings::find()->where(['id'=>1,'status'=>1])->exists())
{
//....... Your code Here ......
}
http://yii2ideas.blogspot.in/2017/06/yii2-database-operations.html