cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Model setupping castOnHydrate and ignoreUnknownColumns problem

Open Zaszczyk opened this issue 10 years ago • 5 comments

I have query:

$qb = $this->modelsManager->createBuilder()
            ->addFrom(ItemLike::class, 'l')
            ->columns('u.*')
            ->join(User::class, 'l.user_id = u.id', 'u')
            ->andWhere('l.item_id = :item_id:', ['item_id' => $itemId])
            ->getQuery()->execute();

and it was working until I added to my code (I added it because I would like to have numbers as integer in objects from DB):

Phalcon\Mvc\Model::setup(['castOnHydrate' => true]);
Phalcon\Mvc\Model::setup(['ignoreUnknownColumns' => true]);

With these settings when I run query results (object Phalcon\Mvc\Model\Resultset\Simple) has invalid columnMap: there are fields from class ItemLike instead of User. So during iteraing results in foreach I got object User but with null fields.

Do You think is right behavior?

I found 2 fixes, but they are problematic to implement in whole application. Fix 1: disable ignoreUnknownColumns and castOnHydrate - columnMap in Phalcon\Mvc\Model\Resultset\Simple is null. During iterating results in foreach object User has right fields. Fix 2: switch class in join() with class in addFrom() - columnMap has fields from User class.

PHP 5.6, Phalcon 3.0.1

Zaszczyk avatar Nov 30 '16 10:11 Zaszczyk

It's not right behaviour, this is well known bug for years/months. The best workaround and solution for now is just:

->columns('l.*,u.*')

Jurigag avatar Dec 02 '16 16:12 Jurigag

This sounds like yet another bug related to ignoreUnknownColumns that's taking dust, like #11907.

If someone wants to give it new life, I created a dockerized project based on Invo (the Phalcon sample project): https://github.com/igorsantos07/invo You can fork it, add the code that triggers the issue to IndexController::indexAction(), and post it here. Would certainly help the maintainers to verify the issue and maybe, fix it.

igorsantos07 avatar Mar 03 '17 03:03 igorsantos07

Bump

magroski avatar Feb 21 '18 13:02 magroski

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

stale[bot] avatar May 22 '18 13:05 stale[bot]

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

stale[bot] avatar Aug 21 '18 21:08 stale[bot]