在自定义TYPO3 8.7.12 extbase扩展中,我无法在模板中使用f:debug
项目。
我们在listAction控制器中,只需:
$institutions = $this->institutionRepository->findAll();
$this->view->assignMultiple([
'institutions' => $institutions,
// ... pagination limit ...
]
);
在模板中:
<f:debug>
{institutions}
</f:debug>
这回来了
#1273753083: Cannot cast object of type "TYPO3\CMS\Extbase\Persistence\Generic\QueryResult" to string.
#1273753083: Cannot cast object of type "TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage" to string.
#1234386924: Cannot create empty instance of the class "TYPO3\CMS\Extbase\Persistence\ObjectStorage" because it does not implement the TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface.
如果我用f:for
和f:debug
遍历{institutions}:
<f:for each="{institutions}" as="institution" iteration="i">
<f:debug>
{institution}
</f:debug>
</f:for>
我得到了对象的第一个属性,例如名字。
编辑:这是由于模型中的__toString()
魔术方法。如果我删除它,而是我得到命名空间和uid STUBR\Extension\Domain\Model\Institution:55
- 这看起来好像没有渲染对象。
等等...... php.net说The __toString() method allows a class to decide how it will react when it is treated like a string
。那么可以将对象作为字符串处理(类型转换?)?
使用属性是正常的,只有在尝试打印整个对象时才会出现问题。
我应该在哪里看?懒加载?有一些延迟加载属性,但不是很多。或者班上可能缺少某些东西?或者是否有解决方法。
PS:
initStorageObjects()
已经在构造函数中调用了回答这个问题;
<f:debug>{institutions}</f:debug>
将被解析为一个对象,但内部的任何空格都会使其解析为一个字符串。
以下方法与<f:debug>
完成相同的工作,并且在我的情况下工作方式类似:
\TYPO3\CMS\Core\Utility\DebugUtility::debug(
$var = $variable,
$header = 'Institutions',
$group = ''
);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(
$variable,
$title = 'Institutions',
$maxDepth = 8,
$plainText = FALSE,
$ansiColors = TRUE,
$return = FALSE,
$blacklistedClassNames = NULL,
$blacklistedPropertyNames = NULL
);
执行列表或在控制器中显示操作。
它比f:debug更不方便(因为你必须在两个不同的地方完成工作,例如当你在模板中的循环中,你必须去控制器并再次构建那个循环),但它是一个有用的解决方法。
编辑:我发现它足够了
<f:debug>{var}</f:debug>
在一条线上