简体   繁体   中英

Doctrine dynamic Entity attributes

In Doctrine2 I have a class that has an attribute that's calculated using a complex SQL query. 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.

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.
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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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