PhP -是否有一个EOF模型::find



我正在写一段代码,在这段代码中,我想知道如果发现结果为空与否。下面是我的代码:

public function signatureAction()
{
    $info = $this->session->get('current_quote');
    $object_list = ApplicationSignatureFile::find(array('conditions' => 'application_id = ?1 AND quote_id = ?2',
        'bind' => [
            1 => $info['application_id'],
            2 => $info['quote_id'],
        ]));
    $this->view->setVar('object_list', $object_list);
    if ($object_list) {
        $this->view->setVar('has_files',true);
    } else {
        $this->view->setVar('has_files',false);
    }
}

我还不知道如果如何检查$object_list是EOF,所以我可以更好地设置has_files变量。目前它不起作用。我如何在控制器和.volt视图中做到这一点?

这实际上很奇怪。使用findFirst或任何其他ORM方法在失败时返回false,但是使用find不会。

在这种情况下,一个简单的解决方法是在结果集上使用count方法:
$test = ModelsObjects::find([
    'conditions' => 'is_active = 42'
]);
if ($test->count()) {
    print('I have records with is_active = 42');
    d($test->toArray());
} else {
    print('I do not have any records with is_active = 42');
}

最新更新