Cphalcon: [2.0.13] "Phalcon\Mvc\Model\Resultset\Simple" can not be iterated after "setHydrateMode()"

Created on 14 Jul 2016  路  8Comments  路  Source: phalcon/cphalcon

If I use mode HYDRATE_ARRAYS ...

        $r = BookModel::find();
        $r->setHydrateMode(\Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS);

        foreach($r as $i) {  // this is line 52
            echo $i['name'];
        }

... I will get errors like:

Warning: Illegal offset type in /Users/guweigang/github/bullsoft/fmyuedu/backend/app/services/BookService.php on line 52

If i use mode HYDRATE_OBJECTS ...

        $r = BookModel::find();
        $r->setHydrateMode(\Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS);

        foreach($r as $i) {  // this is line 52
            echo $i->name;
        }

... I will get errors like:

Warning: Phalcon\Mvc\Model::cloneResultMapHydrate(): Property should be string in /Users/guweigang/github/bullsoft/fmyuedu/backend/app/services/BookService.php on line 52

Notice: Undefined property: stdClass::$name in /Users/guweigang/github/bullsoft/fmyuedu/backend/app/services/BookService.php on line 53

But there is nothing wrong with mode HYDRATE_RECORDS.

Is this a bug?

bug medium

All 8 comments

unit-tests/ModelsHydrationTest.php:132
unit-tests/ModelsHydrationTest.php:139
unit-tests/ModelsHydrationTest.php:146
unit-tests/ModelsHydrationTest.php:165
unit-tests/ModelsHydrationTest.php:172
unit-tests/ModelsHydrationTest.php:178
unit-tests/ModelsHydrationTest.php:202
unit-tests/ModelsHydrationTest.php:209
unit-tests/ModelsHydrationTest.php:216
unit-tests/ModelsHydrationTest.php:235
unit-tests/ModelsHydrationTest.php:242
unit-tests/ModelsHydrationTest.php:250
unit-tests/ModelsHydrationTest.php:271
unit-tests/ModelsHydrationTest.php:277
unit-tests/ModelsHydrationTest.php:283
unit-tests/ModelsHydrationTest.php:300
unit-tests/ModelsHydrationTest.php:310
unit-tests/ModelsHydrationTest.php:320

@sergeyklay what do you mean?

Files and line where setHydrateMode is used (I mean tests). Have you seen this?

Do you use option orm.cast_on_hydrate?
Now phalcon have one bug with Phalcon\Mvc\Model::cloneResultMapHydrate() and this option.

I think you use option orm.cast_on_hydrate, see issue #12191 and pull request #12192.
This PR should solve your problem.

@guweigang

Could you please check 3.0.x branch?

git clone [email protected]:phalcon/cphalcon.git
cd cphalcon
git checkout 3.0.x

zephir fullclean
zephir build

Sorry for the late reply,,,, I have set Model behavior in initialize().

self::setUp([
            "notNullValidations" => false,
            "castOnHydrate" => true,
            "forceCasting" => true
]);

The problem is from castOnHydrate, When i comment that line, everything goes right.

And I just updated to 3.0.x,,, It fixed this issue.

Thx, @sergeyklay @andrew-demb

Was this page helpful?
0 / 5 - 0 ratings