Der*_*n81 6 cakephp cakephp-3.x
以下代码仅返回错误:表"用户"中未找到记录
if($this->Users->get($uid)->isEmpty()) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
因为表是空的,所以如果表为空,我想自定义它,并在浏览器中调用新页面
ndm*_*ndm 10
Table::get()将立即评估查询并返回实体,或者如果具有给定主键的记录不存在则抛出异常,即它不返回查询,则无法调用isEmpty()结果.
如果您正在使用,Table::get()您可以抓住RecordNotFoundException:
try {
$user = $this->Users->get($uid);
// ...
} catch (\Cake\Datasource\Exception\RecordNotFoundException $exeption) {
// record doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
如果您想使用isEmpty(),则需要使用常规查询:
$query = $this->Users->findById($uid);
if ($query->isEmpty()) {
// record doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
如果您实际上不需要结果,可以使用Table::exists():
$exists = $this->Users->exists(['id' => $uid]);
if ($exists !== true) {
// record doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
或COUNT查询:
$count = $this->Users->findById($uid)->count();
if ($count !== 1) {
// record doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
也可以看看
| 归档时间: |
|
| 查看次数: |
1453 次 |
| 最近记录: |