使用Doctrine使用DQL与SQL相比有什么好处? [英] With Doctrine what are the benefits of using DQL over SQL?

查看:133
本文介绍了使用Doctrine使用DQL与SQL相比有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Doctrine类时,需要使用自定义查询时,有人可以提供几个明确的(事实支持的)使用/学习DQL与SQL的理由?



我发现如果我不能使用ORM的内置关系功能来实现我通常在扩展的Doctrine或DoctrineTable类中编写一个自定义方法。在这种方法中,将所需的数据写入直SQL(使用适当准备语句/注入保护等的PDO)。 DQL看起来像额外的语言学习/调试/维护,不出现提供足够的令人信服的理由在大多数常见的情况下使用。 DQL似乎并不比SQL要复杂得多,实际上我怀疑你可以有效地使用DQL,而不必对SQL进行理解。大多数核心SQL语法端口在PHP中可以使用的最常见的数据库中相当不错。



我缺少/俯瞰什么?我相信有一个原因,但是我想听到有意使用它的人,并且获得的优点是尝试使用plain-ole SQL。


$ b $我不是在寻找一个支持ORM的参数,只是在需要在核心get-by-relationship类型需求之外做一些事情时,DQL在传统的LAMP设置中(使用mysql,postgres等) )

解决方案

说实话,我学习SQL使用Doctrine1.2 :)我甚至不了解外键,级联操作,复合函数如group_concat和许多其他事情。索引搜索也是非常好的,方便的事情,只是简单的开箱即用。



DQL编写和理解代码要简单得多。例如,这个查询:

  $ query = ..... //对于类别的一些查询
- > ; leftJoin(c.Products p)

它将在类别和产品之间留下加入,你不必写ON p.category_id = c.id。



如果将来你会改变一对二的关系,让我们说很多2-很多,这个相同的查询将工作没有任何变化。教义将会照顾到这一点。如果您使用SQL执行此操作,则所有查询都必须更改为包含该中介许多2个表。


Can someone provide me a couple clear (fact supported) reasons to use/learn DQL vs. SQL when needing a custom query while working with Doctrine Classes?

I find that if I cannot use an ORM's built-in relational functionality to achieve something I usually write a custom method in the extended Doctrine or DoctrineTable class. In this method write the needed it in straight SQL (using PDO with proper prepared statements/injection protection, etc...). DQL seems like additional language to learn/debug/maintain that doesn't appear provide enough compelling reasons to use under most common situations. DQL does not seem to be much less complex than SQL for that to warrant use--in fact I doubt you could effectively use DQL without already having solid SQL understanding. Most core SQL syntax ports fairly well across the most common DB's you'll use with PHP.

What am I missing/overlooking? I'm sure there is a reason, but I'd like to hear from people who have intentionally used it significantly and what the gain was over trying to work with plain-ole SQL.

I'm not looking for an argument supporting ORMs, just DQL when needing to do something outside the core 'get-by-relationship' type needs, in a traditional LAMP setup (using mysql, postgres, etc...)

解决方案

To be honest, I learned SQL using Doctrine1.2 :) I wasn't even aware of foreign-keys, cascade operations, complex functions like group_concat and many, many other things. Indexed search is also very nice and handy thing that simply works out-of-the-box.

DQL is much simpler to write and understand the code. For example, this query:

$query = ..... // some query for Categories
   ->leftJoin("c.Products p")

It will do left join between Categories and Products and you don't have to write ON p.category_id=c.id.

And if in future you change relation from one-2-many to let's say many-2-many, this same query will work without any changes at all. Doctrine will take care for that. If you would do that using SQL, than all the queries would have to be changed to include that intermediary many-2-many table.

这篇关于使用Doctrine使用DQL与SQL相比有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆