简体   繁体   English

Symfony 2最佳实践DBAL没有对象原则

[英]Symfony 2 best practice DBAL without object doctrine

Well I am finally started to learn Symfony and I think peoples will understand my question (I Hope) and my wish to structure my code... 好吧,我终于开始学习Symfony,我想人们会理解我的问题(我希望)和我希望构建我的代码......

Well, I would like to create a Class which is called Reception and this class has a sql let's say in each methods/function. 好吧,我想创建一个名为Reception的类,这个类在每个方法/函数中都有一个sql。 Each and evry methods can return a different nombre of column results. 每个和每个evry方法都可以返回不同的列结果。

Example : Sql 1 : Jo;DATE; 示例:Sql 1:Jo; DATE; Sql 2 : Client;Car;Time Sql 2:客户端;汽车;时间

Let's tell that I don't want to create a Entity to use it with doctrine... 让我们告诉我,我不想创建一个实体来使用它与学说...

I would like to use DBAL (pdo doctrine sql query) to do execute my queries... like in normal PHP poo programming. 我想使用DBAL(pdo doctrine sql查询)来执行我的查询...就像在普通的PHP poo编程中一样。

Finally the question is : wether I should do this class as a Service, Entity? 最后问题是:我应该把这个类作为服务,实体吗? or I can simple put the pdo query in the controller.... 或者我可以简单地将pdo查询放在控制器中....

Thanks in advance for your answers... I avoid doctrine for the moment because I principaly doing some statistiques and also to play a bit with symfony and to increase the difficutly level progresivly... 在此先感谢您的回答......我暂时避免学说,因为我主要做一些统计学,并且与symfony一起玩,并逐渐增加难度级别......

thanks for your understanding... Good Day 谢谢你的理解......美好的一天

A Service ( docs ) is usually just a class which is responsible for doing some specific task. Servicedocs )通常只是一个负责执行某项特定任务的类。 Lets say you have some statistics you need to be updated whenever specific events occur (ie file is downloaded, favored, etc) and you have multiple controllers where those different events occur. 假设您有一些统计信息需要在发生特定事件时更新(即文件已下载,偏好等),并且您有多个控制器可以发生这些不同的事件。 It would be a really bad idea to simply "copy-paste" your code. 简单地“复制粘贴”代码是一个非常糟糕的主意。 A better way would be to create a service and call it. 更好的方法是创建服务并调用它。

An Entity ( docs ) is an object which represents your database table. Entitydocs )是表示数据库表的对象。 This object can later be used to generate forms in Symfony. 以后可以使用此对象在Symfony中生成表单。 Once you create an Entity , you can then create an EntityRepository . 创建Entity ,您可以创建EntityRepository It is used to store more comprehensive sql queries. 它用于存储更全面的SQL查询。 You can, for instance, have a method like this: 例如,您可以使用以下方法:

public function findUsersWithOrders() {
    // Here you can:
    //  1. use queryBuilder, which generates the query for you
    //  2. write your DQL (Doctrine Query Language) manually
    //  3. write a plain SQL query and return the results
}

I would strongly advice you to use this approach - it will save you a lot of time once you get a hold of it and IMHO is a better coding practice. 我强烈建议你使用这种方法 - 一旦你掌握了它,它将为你节省很多时间,恕我直言是一个更好的编码实践。

If you still decide you would want to pursuit your idea of storing queries in a class: 如果您仍然决定要追求在类中存储查询的想法:

  1. Yes, you could create a Service and use it for that purpose. 是的,您可以创建一个Service并将其用于此目的。 You should use Symfony >= 2.3 because of Lazy Services which optimizes service loading. 您应该使用Symfony> = 2.3,因为Lazy Services可以优化服务加载。 Here is an example of how your service might look 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 WHERE id = 1')->execute(); // Prepare (you can use just query() as well) $select = $this->connection->prepare('SELECT * FROM users WHERE username LIKE :username'); $select->bindValue(':username', '%sample%'); $select->execute(); return $select->fetch(\\PDO::FETCH_ASSOC); } } 

    Then, in your services.yml file you need to put this: 然后,在您的services.yml文件中,您需要将此:

     app.myservicename: class: App\\BaseBundle\\Services\\MyServiceName arguments: [ @doctrine.orm.entity_manager ] 

    Now, whenever you call $this->get('app.myservicename') from a Controller , you will get an instance of your class. 现在,每当您从Controller调用$this->get('app.myservicename') ,您将获得您的类的实例。

  2. Of course, you can put your sql code in the controller as well. 当然,您也可以将您的sql代码放在控制器中。 This is not a good practice and you should avoid doing it, though. 这不是一个好习惯,但你应该避免这样做。 This example shows you how to do it: 此示例显示了如何执行此操作:

     /** * @Route("/some/route") * @Template() */ public function indexAction() { /** * @var \\Doctrine\\ORM\\EntityManager $em */ $em = $this->getDoctrine()->getManager(); // some business logic ... try { $em->getConnection()->query('UPDATE statistics SET counter=counter+1 WHERE id = 1')->execute(); } catch(\\Doctrine\\DBAL\\DBALException $e){ // the query might fail, catch the exception and do something with it. } // other business logic... return array('name' => 'Hello World'); } 

I would advice you to have a look at the symfony best practices to see what are the best approaches to common problems. 我建议你看看symfony最佳实践 ,看看哪些是解决常见问题的最佳方法。 Also reading the main documentation will help you clear a lot of questions. 阅读主要文档也可以帮助您清除很多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM