Friday 15 August 2014

php - use query builder or sql doctrine -



php - use query builder or sql doctrine -

i research reply don't find.

i'd know best practice select (with join) between :

use query builder ?

$this->getentitymanager()->createquerybuilder() ->select('e') ->from('module\entity\myentity 'e') ->innerjoin('module\entity\myentity2', 'e2', 'with', 'e.e2_id = e2.id')... ->where("...")

or

use sql statement ?

$db = $this->getentitymanager()->getconnection(); $sql = "select * myentity e inner bring together myentity2 e2 on e2.id = e.e2_id.... ....;"

it safer, faster,... ?

both have advantages , disadvantages, depends on need.

sql

a pure sql statement little bit faster, since don't have execute additional logic coming doctrine since doctrine tries back upwards lot of different databases, database specific functions not supported, can't utilize them or have implement them doctrine, can become lot of work

dql

doctrine forces utilize prepared statements, making prevention of injection attacks easier enforce long insist on named parameters. can done pure sql , pdo too, using named parameters. working entities much easier. info automatically bound objects , managed through application. of course of study causes nail performance overhead one disadvantage regarding joins bring together whole table instead of maybe 2 columns need. , need relationships defined if result should come in object-useful way biggest benefit rarest used: if alter database don't need rewrite queries match new query structure

sometimes find myself in situation solve problem straight in sql query, doctrine doesn't back upwards of constructs have use. have decide if want lose doctrine benefits , go pure sql solution or utilize dql , add together more php code, maybe more otherwise unnecessary queries. depends on situation , can not answered in general.

in end utilize dql wherever possible because it's easier write , maintain , switch sql when need query high performance.

php mysql performance doctrine2 query-builder

No comments:

Post a Comment