Comment vérifier l'existence d'un modèle dans DB? Dans Yii 1 version, il en était ainsi User::model()->exist()
Dans Yii2, vous pouvez ajouter exists()
à votre chaîne de requête:
User::find()
->where( [ 'id' => 1 ] )
->exists();
(Le SQL généré ressemble à ceci: SELECT 1 FROM `tbl_user` WHERE `id`=1
.)
Voici Query->exists()
de la source Yii2 :
/**
* 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 bool whether the query result contains any row of data.
*/
public function exists($db = null)
{
if ($this->emulateExecution) {
return false;
}
$command = $this->createCommand($db);
$params = $command->params;
$command->setSql($command->db->getQueryBuilder()->selectExists($command->getSql()));
$command->bindValues($params);
return (bool) $command->queryScalar();
}
if(Mastersettings::find()->where(['id'=>1,'status'=>1])->exists())
{
//....... Your code Here ......
}
http://yii2ideas.blogspot.in/2017/06/yii2-database-operations.html