php - Symfony 2 best practice DBAL without object doctrine -
well started learn symfony , think peoples understand question (i hope) , wish structure code...
well, create class called reception , class has sql let's in each methods/function. each , evry methods can return different nombre of column results.
example : sql 1 : jo;date; sql 2 : client;car;time
let's tell don't want create entity use doctrine...
i use dbal (pdo doctrine sql query) execute queries... in normal php poo programming.
finally question : wether should class service, entity? or can simple put pdo query in controller....
thanks in advance answers... avoid doctrine moment because principaly doing statistiques , play bit symfony , increase difficutly level progresivly...
thanks understanding... day
a service
(docs) class responsible doing specific task. lets have statistics need updated whenever specific events occur (i.e. file downloaded, favored, etc) , have multiple controllers different events occur. bad idea "copy-paste" code. better way create service , call it.
an entity
(docs) object represents database table. object can later used generate forms in symfony. once create entity
, can create entityrepository
. used store more comprehensive sql queries. can, instance, have method this:
public function finduserswithorders() { // here can: // 1. use querybuilder, generates query // 2. write dql (doctrine query language) manually // 3. write plain sql query , return results }
i advice use approach - save lot of time once hold of , imho better coding practice.
if still decide want pursuit idea of storing queries in class:
yes, create
service
, use purpose. should use symfony >= 2.3 because of lazy services optimizes service loading. here example of how service might like:// app\basebundle\services\myservicename.php namespace app\basebundle\services; use doctrine\orm\entitymanager; class myservicename { /** * @var \doctrine\orm\entitymanager */ private $em; /** * @var \doctrine\dbal\connection */ private $connection; public function __construct(entitymanager $entitymanager) { $this->em = $entitymanager; $this->connection = $entitymanager->getconnection(); } public function getusers(){ // update query $this->connection->query('update statistics set counter=counter+1 id = 1')->execute(); // prepare (you can use query() well) $select = $this->connection->prepare('select * users username :username'); $select->bindvalue(':username', '%sample%'); $select->execute(); return $select->fetch(\pdo::fetch_assoc); } }
then, in
services.yml
file need put this:app.myservicename: class: app\basebundle\services\myservicename arguments: [ @doctrine.orm.entity_manager ]
now, whenever call
$this->get('app.myservicename')
controller
, instance of class.of course, can put sql code in controller well. not practice , should avoid doing it, though. example shows how it:
/** * @route("/some/route") * @template() */ public function indexaction() { /** * @var \doctrine\orm\entitymanager $em */ $em = $this->getdoctrine()->getmanager(); // business logic ... try { $em->getconnection()->query('update statistics set counter=counter+1 id = 1')->execute(); } catch(\doctrine\dbal\dbalexception $e){ // query might fail, catch exception , it. } // other business logic... return array('name' => 'hello world'); }
i advice have @ symfony best practices see best approaches common problems. reading main documentation clear lot of questions.
Comments
Post a Comment