1

I'm writing a piece of code and in it I would like to know if the result of a find if empty or not. Here is my piece of code:

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);
    }
}

What I don't know yet if how to check if $object_list is EOF so I can set the has_files variable better. Currently it is not working. How can I do that in a controller and in a .volt view?

4

1 回答 1

1

这实际上很奇怪。usingfindFirst或任何其他 ORM 方法false在失败时返回,但 usingfind不会。

在您的情况下,一个简单的解决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');
}
于 2016-08-30T04:57:39.720 回答