Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Unexpected results when using magic getters

Hi,

Phalcon ORM's magic getters don't always return the results I expect. See these examples:

Countries           Cities
+----+--------+     +----+------------+------------+------------+
| id | name   |     | id | name       | country_id | is_capital |
+----+--------+     +----+------------+------------+------------+
|  1 | UK     |     |  1 | London     |          1 |          1 |
+----+--------+     |  2 | Manchester |          1 |          0 |
                    |  3 | Liverpool  |          1 |          0 |
                    +----+------------+------------+------------+

class Countries extends Model {
    public $id;
    public $name;

    public function initialize() {
        $this->hasMany('id', 'Cities', 'country_id', [
            'alias' => 'Cities'
        ]);
    }
}

class Cities extends Model {
    public $id;
    public $name;
    public $country_id;
    public $is_capital;

    public function initialize() {
        $this->belongsTo('country_id', 'Countries', 'id', [
            'alias' => 'Country'
        ]);
    }
}

$uk = Countries::findFirstByName("UK");

$r1 = $uk->getCities("is_capital = 1");
$r2 = $uk->getCities(['is_capital' => 1]);
// $r1 expectingly returns "London"
// $r2 surprisingly returns "London Manchester Liverpool"

$r3 = $uk->getCities("is_capital = 0");
$r4 = $uk->getCities(['is_capital' => 0]);
// $r3 expectingly returns "Manchester Liverpool"
// $r4 surprisingly returns "London Manchester Liverpool"

$r5 = $uk->getCities("is_capital = 'blabla'");
$r6 = $uk->getCities(['is_capital' => 'blabla']);
// $r5 surprisingly returns "Manchester Liverpool"
// $r6 surprisingly returns "London Manchester Liverpool"

Should I conclude that:

  • passing an array of parameters to a magic getter is not reliable? ($r2, $r4, $r6)
  • and even when passing in a string, I can sometimes get surprises? ($r5)

Or am I missing something?

Thanks!



85.3k
Accepted
answer

These syntax aren't supported by the ORM:

$r2 = $uk->getCities(['is_capital' => 1]);
$r4 = $uk->getCities(['is_capital' => 0]);
$r6 = $uk->getCities(['is_capital' => 'blabla'])


5.5k

OK, @Phalcon, thanks.

Do you have any explanation about my second point (the result I get in $r5)?

I don't know exactly how the magic resolves the query in Phalcon but maybe its PHP logic where a string for comparison is converted to 0.



85.3k

MySQL applies type coercion converting the "blablabla" to 0:

mysql> select 0 = "blablabla";
+-----------------+
| 0 = "blablabla" |
+-----------------+
|               1 |
+-----------------+
1 row in set, 1 warning (0.01 sec)

OK, @Phalcon, thanks.

Do you have any explanation about my second point (the result I get in $r5)?



5.5k

OK, of course, stupid question (facepalm). Thanks anyway for your answers.