Cphalcon: Bad result when using Model findFirst().

Created on 7 Aug 2018  路  7Comments  路  Source: phalcon/cphalcon

Expected and Actual Behavior

The test script is ok when without line 2, when add the line2, findFirst will return the first row in database which is not related to the condition.

Provide minimal script to reproduce the issue

$parameters = [[["id=470"]]];
$conditions = &$parameters[0];  // the problem code
$model = Model::findFirst($parameters);

Details

  • Phalcon version: (3.4.0)
  • PHP Version: (7.2.6)
  • Operating System: macOS 10.13.6
  • Installation type: Compiling from source || installing via package manager
  • Zephir version (if any):
  • Server: Nginx
  • Other related info (Database, table schema):
not a bug

Most helpful comment

It's very strange, the code runs well now with the reference, I used test it on my workmate鈥榮 computer too, but both ok now.
Sorry to trouble you, really appreciate for your reply.

All 7 comments

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

You are passing the $parameters[0] by reference. (see the &). Also you have a 3 level nested array there so Phalcon will never match the contents of that array to findFirst.

You are passing the $parameters[0] by reference. (see the &). Also you have a 3 level nested array there so Phalcon will never match the contents of that array to findFirst.

Thanks for your reply, but I don't quite understand it. Phalcon support a 3 level nested array parameter, if I remove the second line, the findFirst works well, also the second line doing nothing to the $parameters, just a reference to it.

Check what happens if you do this:

$parameters = [[["id=470"]]];
$conditions = $parameters[0]; // <- Note here there is no "&"
$model = Model::findFirst($parameters);

You can also do this

$model = Model::findFirst(
[
    'conditions' => 'id = :id:',
    'bind'       => [
        'id' => 470,
    ],
);

It's very strange, the code runs well now with the reference, I used test it on my workmate鈥榮 computer too, but both ok now.
Sorry to trouble you, really appreciate for your reply.

By accident, when I was reading a document about php internal, I guess I got the answer. A reference could change the php value's is_ref property saved in zval, maybe phalcon did something different when that property is changed.

$foo['love'] = 1;
$bar = &$foo['love'];
$tipi = $foo;
$tipi['love'] = '2';
echo $foo['love'];

the result is '2'.
copy from: http://www.php-internals.com/book/?p=chapt06/06-06-copy-on-write

And also, I test the code I post in my question again, it runs not well again, I doubt whether it ever runs well or I make a mistake the last time I test it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ruudboon picture ruudboon  路  3Comments

ismail0234 picture ismail0234  路  3Comments

bestirani2 picture bestirani2  路  3Comments

kkstun picture kkstun  路  3Comments

borisdelev picture borisdelev  路  3Comments