简体   繁体   English

教义动态实体属性

[英]Doctrine dynamic Entity attributes

In Doctrine2 I have a class that has an attribute that's calculated using a complex SQL query. 在Doctrine2中,我有一个类,该类具有使用复杂的SQL查询计算的属性。 That attribute is called "duplicate" 该属性称为“重复”

I don't want to persist this value in my database and I want it to be calculated everytime I hydrate a given object and access it using 我不想将此值保留在数据库中,并且希望每次对给定对象进行水合并使用进行访问时都进行计算

$obj->getDuplicate()

If I add a separate select to my DQL that'd result in an array hydration, that's why I want this parameter to be pretty much part of my object entity, but not persisted. 如果我在DQL中添加一个单独的选择,这会导致数组水化,这就是为什么我希望此参数在我的对象实体中占很大一部分,而不是持久存在的原因。

What are my options? 我有什么选择?

(we're planning to move to array hydration all together due to performance reasons in next version but no time now :) (由于性能原因,我们计划在下一个版本中一起迁移至阵列水合,但现在没有时间:)

You can use a postLoad event listener and inject the repository there. 您可以使用postLoad事件侦听器,然后将存储库注入那里。
Once you have the repository, you can call your custom DQL query that will do the job for you, everytime you load an object from database. 有了存储库后,每次从数据库加载对象时,都可以调用自定义DQL查询,该查询将为您完成此工作。

I don't know, however, if this is a good practice, but I'm sure that this will work. 但是,我不知道这是否是个好习惯,但我敢肯定这会奏效。

Take a look here if you want some hint. 如果您需要一些提示,请看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM