mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Can't combine fetch: EXTRA_LAZY with indexBy: foreign key #5235
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @dragoste on GitHub (Aug 26, 2016).
I found it impossible to use
EXTRA_LAZYmode for collection indexed by a relation (foreign key). It may be related to #3978.Let's suppose we have
Parentclass withParent::childrencollection whereChild::useris unique across the collection (oneChildperUserfor eachParent). When we would like to indexParent::childrenbyChild::userproperty (user_idactually), it works for standardfetch:LAZYmode as implemented in PR #639, which lets us to use column name instead of property name.For
EXTRA_LAZYneither int/string (user_idin this case) noruser(Userentity) works as a key when usingcontainsKeyon collection, regardless if it'sindexBy: user_idorindexBy: user.If I do
$this->children->containsKey($user)(passUserentity as key) it will stop atArrayCollection::containsKey()withWarning: Illegal offset type in isset or emptyin line:That's obvious. So if I do
$this->children->containsKey($user->getId()), then in case of mappingindexBy: user_id(column name) I get:And in case of
indexBy: userit fails ofSqlExpressionVisitor::walkComparisionwith:because it expects an object here (which can't pass because it fails earlier).
Doctrine2 version 2.5.4
PHP 7.0.6
@ihor commented on GitHub (Jun 15, 2018):
Did you find a solution to this?
@dragoste commented on GitHub (Jun 15, 2018):
@ihor Nope. I tried to debug Doctrine's code and I think there's no workaround to keep both
EXTRA_LAZYandindexBy: foreign_keyas it's messed up in Doctrine core codebase.Since there was no response to this issue, I finally decided to give up on
EXTRA_LAZYin my case.@ajgarlag commented on GitHub (Apr 4, 2022):
Since ORM 2.11 you can expose the
foreign_key(user_id) column as a virtual column and use it as the indexed field of anEXTRA_LAZYassociation. See https://www.doctrine-project.org/2022/01/11/orm-2.11.html