With Doctrine what are the benefits of using DQL over SQL?

旧巷老猫 提交于 2019-12-04 05:20:28

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.

I find DQL more readable and handy. If you configure it correctly, it will be easier to join objects and queries will be easier to write.

Your code will be easy to migrate to any RDBMS.

And most important, DQL is object query language for your object model, not for your relational schema.

Using DQL helps you to deal with Objects. in case inserting into databae , you will insert an Object

$test = new Test();
$test->attr = 'test';
$test->save();

in case of selecting from databae, you will select an array and then you can fill it in your Object

public function getTestParam($testParam)
     {
        $q=Doctrine_Query::create()
                ->select('t.test_id , t.attr')
                ->from('Test t ')
            $p = $q->execute();
            return $p;
     }

you can check the Doctrine Documentation for more details

Zeljko's answer is pretty spot-on.

Most important reason to go with DQL instead of raw SQL (in my book): Doctrine separates entity from the way it is persisted in database, which means that entities should not have to change as underlying storage changes. That, in turn, means that if you ever wish to make changes on the underlying storage (i.e. renaming columns, altering relationships), you don't have to touch your DQL, because in DQL you use entity properties instead (which only happen to be translated behind the scenes to correct SQL, depending on your current mappings).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!