When using the MQL object in ThinkPHP5, we sometimes need to determine whether the object is empty. This article will explore how to determine whether an MQL object is empty.
In ThinkPHP5, each model has a default MQL object. We can obtain this object through the static method of the model, such as:
$userModel = new \app\user\model\UserModel; $userModel->where('username', 'like', '%admin%')->select();
can also be written as:
$userModel = \app\user\model\UserModel::where('username', 'like', '%admin%')->select();
The MQL object provides the count() method for querying records that meet the conditions. quantity. If the number of records returned is 0, the MQL object is empty.
$userModel = \app\user\model\UserModel::where('username', 'like', '%notexist%'); if($userModel->count() == 0){ echo 'MQL对象为空'; }
The MQL object provides the find() method to query the first record that meets the conditions. If the returned result is null, it means that the MQL object is empty.
$userModel = \app\user\model\UserModel::where('username', 'like', '%notexist%')->find(); if(is_null($userModel)){ echo 'MQL对象为空'; }
The MQL object provides the select() method to query all records that meet the conditions. If the returned result is an empty array, it means that the MQL object is empty.
$userModel = \app\user\model\UserModel::where('username', 'like', '%notexist%')->select(); if(empty($userModel)){ echo 'MQL对象为空'; }
The MQL object provides the isEmpty() method, which is used to determine whether the MQL object is empty. If the return result is true, it means that the MQL object is empty.
$userModel = \app\user\model\UserModel::where('username', 'like', '%notexist%'); if($userModel->isEmpty()){ echo 'MQL对象为空'; }
The above is the detailed content of thinkphp5 discusses how to determine whether an MQL object is empty. For more information, please follow other related articles on the PHP Chinese website!