如何检查数据库中是否存在模型? 在Yii的1版本,它是如此 User::model()->exist()
Yii2:检查数据库中是否存在ActiveRecord模型
18
A
回答
39
在Yii2您可以添加exists()
到您的查询链:
User::find()
->where([ 'id' => 1 ])
->exists();
(生成的SQL是这样的:SELECT 1 FROM `tbl_user` WHERE `id`=1
)
这里也是Query->exists()
取自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
相关问题
- 1. Yii2检查数据库中是否已存在电子邮件
- 2. 如何从模型中检查ActiveRecord中是否存在关联?
- 3. 如何使用Yii2检查数据库中的数据是否存在?
- 4. Yii2:如何检查是否ActiveRecord的型号是空
- 5. 检查表是否存在于数据库中,但不在模型中
- 6. 检查数据库是否存在
- 7. JavaDB - 检查数据库是否存在
- 8. 检查SQLite数据库是否存在
- 9. MySQL检查数据库是否存在
- 10. 检查数据库中是否存在数据库
- 11. 在创建新模型之前,FactoryGirl能否检查数据库中是否已存在模型?
- 12. Cakephp检查模型是否存在
- 13. 检查模型是否存在
- 14. 红宝石检查数据库中是否存在数据
- 15. 如何检查数据库中是否存在数据?
- 16. 检查数据库中是否存在数据
- 17. 检查存储过程是否存在于数据库中?
- 18. 检查数据库中是否存在使用查询> NodeJS
- 19. 查询检查访问数据库中是否存在表
- 20. php检查数据库中是否存在数字
- 21. 检查数据库表中是否存在已有的数字?
- 22. PHP ActiveRecord如何检查连接数据库是否成功?
- 23. Rails,ActiveRecord,Squeel:检查数组中是否存在值
- 24. 如何从数据库中检查列是否存在表中?
- 25. 如何检查在Access数据库中是否存在记录
- 26. 在Android中,检查sqlite数据库是否存在失败
- 27. 检查用户是否登录在数据库中存在
- 28. 检查MySQL数据库数据是否不存在
- 29. 检查数据是否存在于数据库VB.Net
- 30. 查询Firebase用户是否检查数据库中是否存在联系人