#90215Getters of class LazyLoadingProxy can't be called by Fluid
Description
I would like to point to an closed issue with the same headline, since the problem still exists: Bug #87651.
In TYPO3 version 9.5.13 (non composer installation), the n:1 relation can’t be resolved in fluid templates.
Reviews 3
Related issues 2
History 1821
-
Chris no-lastname-given changed Related to #87651 <span data-processor="relativetime" data-value="2020-01-27T15:10:12+00:00" > 01/27/20, 15:10 </span>
-
Wolfgang Klinger commented and changed priority
Should haveMust have <span data-processor="relativetime" data-value="2020-01-30T08:47:09+00:00" > 01/30/20, 08:47 </span> -
Guillaume Germain commented <span data-processor="relativetime" data-value="2020-02-17T10:13:07+00:00" > 02/17/20, 10:13 </span>
I also see the problem with TYPO3 9.5.13 and typo3fluid/fluid 2.6.9 in Composer mode.
This bug has an impact on performances.
-
Alexander Vogt commented <span data-processor="relativetime" data-value="2020-02-17T10:28:25+00:00" > 02/17/20, 10:28 </span>
We have the same issue (9.5.13, composer mode). Currently we are using a viewhelper as workaround. For example:
<site:lazyLoad model="{relation.department}" />... public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) { if($arguments['model'] instanceof LazyLoadingProxy) { $arguments['model']->_loadRealInstance(); } } ... -
Johannes Rebhan commented <span data-processor="relativetime" data-value="2020-02-18T13:39:32+00:00" > 02/18/20, 13:39 </span>
We have the same problem. A silly workaround seems to be
<f:if condition="{element.lazy} == null"> ... do something with {element.lazy.something} </f:if>This seems to trigger the Proxy and initialize the subobject.
-
B. Kausch commented <span data-processor="relativetime" data-value="2020-03-03T21:18:08+00:00" > 03/03/20, 21:18 </span>
That is pretty nasty… can please somebody fix this?
-
B. Kausch commented <span data-processor="relativetime" data-value="2020-03-03T21:25:21+00:00" > 03/03/20, 21:25 </span>
Guillaume Germain wrote:
> I also see the problem with TYPO3 9.5.13 and typo3fluid/fluid 2.6.9 in Composer mode.
>
> This bug has an impact on performances.Same problem here. Have found the bug: https://github.com/TYPO3/Fluid/issues/513
It only occurs in composer mode when the LazyLoadingProxy is a standard/root variable.
-
B. Kausch commented and changed subject
Lazy loading (sometimes) not working in Fluid (only n:1 relations)Getters of class LazyLoadingProxy can't be called by Fluid <span data-processor="relativetime" data-value="2020-03-04T19:13:41+00:00" > 03/04/20, 19:13 </span>Maintainer of the Fluid library says, the problem should be fixed inside of the Typo3 core:
> As such, the problem is already placed on the implementer instead of the library. The library imposes the restriction that overloaded methods don’t work – the implementer then has to provide a formalised way to avoid depending on overloaded methods to do the work. One such method in TYPO3 CMS context is to provide a virtual, secondary getter for methods/properties that would return incompatible overloaded method implementers. The virtual getter would then either forcibly read the data in a way that triggers the overloaded method, or call formalised methods that perform the necessary action (in this case, thaws the lazy storage).
Reasoning behind this: https://github.com/TYPO3/Fluid/pull/486#issuecomment-545661652
-
Georg Ringer changed status
NewAccepted <span data-processor="relativetime" data-value="2020-03-05T19:20:10+00:00" > 03/05/20, 19:20 </span> -
B. Kausch commented <span data-processor="relativetime" data-value="2020-03-05T23:38:37+00:00" > 03/05/20, 23:38 </span>
Ideas for a solution: https://github.com/TYPO3/Fluid/issues/513#issuecomment-594823546
-
Claus Due commented and changed category <span data-processor="relativetime" data-value="2020-03-07T10:14:31+00:00" > 03/07/20, 10:14 </span>
Moving issue to Extbase category to be resolved there.
Suggested compatibility approach: use ArrayAccess on LazyLoadingProxy to trigger the loading and proxy to the thawed object when a property is attempted accessed on the proxy.
See for reference:
https://github.com/TYPO3/Fluid/issues/513#issuecomment-594823546
-
Rémy DANIEL commented <span data-processor="relativetime" data-value="2020-05-04T17:05:12+00:00" > 05/04/20, 17:05 </span>
Maybe I am to naive, but does adding thoses lines to LazyLoadingProxy will do the job?
class LazyLoadingProxy implements \Iterator, LoadingStrategyInterface, \ArrayAccess // ... public function offsetExists($offset) { return $this->__isset($offset); } public function offsetGet($offset) { return $this->__get($offset); } public function offsetSet($offset, $value) { return $this->__set($offset, $value); } public function offsetUnset($offset) { return $this->__unset($offset); } }I’ve added those line on master, and ran successfully Extbase’s unit and functional tests.
I’ve also tested it on a real project, and my “fluid” issue was solved.In the meantime, would it be okay-ish to use the $this->_loadRealInstance() inside the getters of @lazy properties?
-
Markus Gerdes commented <span data-processor="relativetime" data-value="2020-05-06T13:17:28+00:00" > 05/06/20, 13:17 </span>
Hi Daniel,
for me your workaround does not work completely.My LazyLoaded object inherits some properties from its parent class. Those could not be printed whereas the normal properties show up after your workaround.
DANIEL Rémy wrote:
> Maybe I am to naive, but does adding thoses lines to LazyLoadingProxy will do the job?
>
> […]
>
> I’ve added those line on master, and ran successfully Extbase’s unit and functional tests.
> I’ve also tested it on a real project, and my “fluid” issue was solved.
>
> In the meantime, would it be okay-ish to use the $this->_loadRealInstance() inside the getters of @lazy properties? -
Stephan Großberndt changed Related to #87899 <span data-processor="relativetime" data-value="2020-09-21T16:45:39+00:00" > 09/21/20, 16:45 </span>
-
Gerrit Code Review pushed patch set 1 for master to change 65803 status
AcceptedUnder Review <span data-processor="relativetime" data-value="2020-09-22T10:36:29+00:00" > 09/22/20, 10:36 </span> -
Stephan Großberndt commented <span data-processor="relativetime" data-value="2020-09-22T10:40:39+00:00" > 09/22/20, 10:40 </span>
The proposed patch makes
AbstractDomainObjectimplement\ArrayAccessand load the real extbase domain model hidden behind aLazyLoadingProxyon callingoffsetGet(). This makes all magic getters of extbase domain models withlazy@ annotation available for rendering in Fluid again if they haven’t been resolved yet, which was broken since the removal ofCmsVariableProviderin TYPO3 v9.0 in https://review.typo3.org/c/Packages/TYPO3.CMS/+/53227Fluid will only resolve the properties that are actually used in the template, and only once – once resolved, the property value is changed (because
LazyLoadingProxyviolates visibility and sets it on the parent instance through forced reflection).This is obviously also broken in v9.5 but will not be fixed there anymore as the version only receives security relevant bugfixes.
This solution also fixes the case of lazy loaded objects inheriting properties from their parent class from https://forge.typo3.org/issues/90215#note-13
In order to reproduce the issue you need:
class \MyVendor\MyExtension\Domain\Model\MyChild extends \MyVendor\MyExtension\Domain\Model\MyParent { } class \MyVendor\MyExtension\Domain\Model\MyParent extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity { /** * @var \MyVendor\MyExtension\Domain\Model\MyReference * @lazy */ protected $myReference; }If you put a
MyChildinstance containing a validMyReferenceinto a view$this->view->assign('myChild', $myChild);and accessing{myChild.myReference.uid}in a Fluid template this will return an empty uid in Fluid without the patch. With the patch the UID is there. -
Gerrit Code Review pushed patch set 2 for master to change 65803 <span data-processor="relativetime" data-value="2020-09-22T12:06:19+00:00" > 09/22/20, 12:06 </span>
-
Gerrit Code Review pushed patch set 3 for master to change 65803 <span data-processor="relativetime" data-value="2020-09-22T12:09:58+00:00" > 09/22/20, 12:09 </span>
-
Stephan Großberndt commented and changed status
Under ReviewAccepted <span data-processor="relativetime" data-value="2020-09-22T14:29:06+00:00" > 09/22/20, 14:29 </span>Patch was abandoned as implementing
\ArrayAccessonAbstractDomainObjectlead to problems withObjectAccessandJsonViewof Extbase. -
Stephan Großberndt commented <span data-processor="relativetime" data-value="2020-09-22T18:50:10+00:00" > 09/22/20, 18:50 </span>
Reviving the
CmsVariableProviderclass from https://review.typo3.org/c/Packages/TYPO3.CMS/+/53227/4/typo3/sysext/fluid/Classes/Core/Variables/CmsVariableProvider.php solves the issue for me in TYPO3 v9 (methodresolveSubVariableReferences()is not needed as it is the same inStandardVariableProvider).But this is a poor workaround only for a TYPO3 v9, which is a version that only receives security updates nowadays, so this is only valuable knowledge for people stuck to v9 at the moment. If you want to upgrade to v10 anyway (which you should), this will not help, as there were changes in the reflection area of Extbase and according to Claus Due this will probably break it – I did not test it though.
The better way is to explicitly resolve the
LazyLoadingProxyin your domain model:Change your model from
class \MyVendor\MyExtension\Domain\Model\MyModel extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity { /** * @var \MyVendor\MyExtension\Domain\Model\MyReference * @lazy */ protected $myReference; /** * @return TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy|\MyVendor\MyExtension\Domain\Model\MyReference */ public function getMyReference() { return $this->myReference; } }to
class \MyVendor\MyExtension\Domain\Model\MyModel extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity { /** * @var \MyVendor\MyExtension\Domain\Model\MyReference * @TYPO3\CMS\Extbase\Annotation\ORM\Lazy */ protected $myReference; /** * @return \MyVendor\MyExtension\Domain\Model\MyReference */ public function getMyReference() { if ($this->myReference instanceof LazyLoadingProxy) { $this->myReference->_loadRealInstance(); } return $this->myReference; } }Now you are able to use strict types on the reference, do not have to care about
LazyLoadingProxyanywhere else in your controllers or services and the getters work in Fluid too as if the getter is accessed always the real instance is returned.This of course means you no longer work with a
LazyLoadingProxyat all and do not profit from changes like Allow fetching uid of a LazyLoadingProxy without loading the object firstBy the way: This is only an issue with
LazyLoadingProxy, NOT withLazyObjectStorage, there no such explicit loading is necessary. -
Georg Ringer commented and changed status
AcceptedClosed <span data-processor="relativetime" data-value="2026-02-07T08:34:58+00:00" > 02/07/26, 08:34 </span>I couldn’t reproduce that anymore and haven’t seen any issues in last years regarding that. if you still have the issue in supported versions, please contact me on slack
I can confirm this in composer mode too … 9.5.13, typo3fluid/fluid 2.6.8
If I use a view helper like
<f:groupedFor groupBy="department" groupKey="department" each="{person.roles}" as="relations"> <f:for each="{relations -> v:iterator.sort(sortBy: 'role.priority', order: 'DESC')}" as="relation"> {relation.role.name}, {relation.department.name}<br> </f:for> </f:groupedFor>{relation.role.name} is output (loaded because of the v:iterator.sort view helper), but {relation.department.name} is not