【问题标题】:Phalcon PhP - is there an EOF for model::findPhalcon PhP - 是否有模型的 EOF::find
【发布时间】:2016-08-29 23:16:25
【问题描述】:

我正在编写一段代码,在其中我想知道 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 视图中做到这一点?

【问题讨论】:

    标签: php phalcon eof phalcon-orm


    【解决方案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');
    }
    

    【讨论】:

    • 我认为这可能是 phalcon 和 php 7 的错误,也有,但没有时间检查是什么原因造成的。
    • @Juri 在家里使用 Phalcon 2 和 PHP 5.6 进行了测试。
    • @Juri,我不认为这是一个错误,而是预期的行为。 find 返回一个空的 ResultsetInterface(一个 loopable 对象),它意味着被遍历。例如;如果您执行$findResultset->getFirst(),它将获得您的假定值false
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2019-01-06
    相关资源
    最近更新 更多