Phalcon PhP - 有一个EOF for model :: find

时间:2016-08-29 23:16:25

标签: php phalcon eof phalcon-orm

我正在编写一段代码,并且在其中我想知道查找的结果是否为空。这是我的代码:

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视图中执行此操作?

1 个答案:

答案 0 :(得分:1)

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

在您的情况下,一个简单的解决方法是在结果集上使用count方法:

$test = \Models\Objects::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');
}