We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model::count bug

If I send an empty row to the count() or find() functions, Phalcon throws an error. For example:

\Model::count('', ['bind' => []]);

Is this a bug? I use version 1.1.0.

Count expects valid parameters, so by passing empty variables, the expected behavior is to get an error back.

Ok. Example:

$condition = '';
$bind = [];
if (...) {
    $condition .= ...;
}
if (...) {
    $condition .= ...;
    $bind['...'] = '...';
    $bind['...'] = '...';
}
if (...) {
    $condition .= ...;
    $bind['...'] = '...';
}

How I should build call to count() function?

Only like this:

if ($condition) {
    if (sizeof($bind)) {
        $count = \MyModel::count($condition, ['bind' => $bind]);
    } else {
        $count = \MyModel::count($condition);
    }
} else {
    $count = \MyModel::count();
}

Not cool.

Why isn't the above cool? Because of the fact that there is a conditional there? (if etc.)

This code is not cool because I should call count() function three times instead of one simple calling

$count = \MyModel::count($condition, ['bind' => $bind]);

You don't call count three times. You only call it once per execution. This is why you have the conditionals to tell your code where to go.

One thing to note. Count accepts a single parameter which is an array not two as you posted. So your call should be:

\MyModel::count([$condition, ['bind' => $bind]]);

I am going to add a NFR to ignore empty parameters when passed in that call.



32.5k

Actually, it would be called only once... because all the if/else can't be true at the same time... If you don't want to type function three times it is your personal whims :P

Try to create some kind of impossible condition for its defaults. Like ISNULL(some_not_null_field),

it would be called only once I known it.

not two as you posted I wrote this examples from memory, not from IDE.

I am going to add a NFR to ignore empty parameters when passed in that call. Thanks.