开发者

Is there a trick with Doctrine's Query Language to access object methods?

I have a few methods on my entity class (calculating time intervals between creation开发者_高级运维 and deadline, etc.) that I'd like to order by in my DQL. I tried using __get to access the method as a property but it seems DQL uses get_object_properties or similar so this doesn't work.

Example query that didn't work: SELECT t FROM Entity\Task t ORDER BY t.TTL ASC, where TTL is accessed through __get() which returns getTTL().

Is there a trick I can use to access them in this way, or am I better off just sorting the result set myself?


Not possible. To order by computed values, pre-compute the values and store them in a field. Pre-computing the values will have the added benefit of much more efficient and responsive lookups.

Keep in mind that DQL maps to SQL. To do what you are asking, the DQL would have to retrieve every matching record from the entire table, hydrate them into your entity objects, run your functions and then sort in memory (or re-run the query). It's a nice idea, but it just won't work.

If you don't like the idea of pre-computing the values, for most of these computations you could probably also calculate them inline in the DQL - i.e. order by the difference between two field values. The important point being that it would have to be inline in the DQL rather than calling a compute function on the entity object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜