简体   繁体   English

在控制器中打开数据库连接(连接模式)

[英]Opening database connection in controller (connection pattern)

I've got to implement a database connection pattern. 我必须实现数据库连接模式。 I created two classes: 我创建了两个类:

  • ConnectionFactory 连接工厂
  • MySQLService MySQL服务

ConnectionFactory: ConnectionFactory:

<?php

  class ConnectionFactory {
    protected static $connection;

    public function getConnection() {
        if (!self::$connection) {
            self::$connection = new PDO('mysql:host=localhost;dbname=sapienter', "root", "admin");
        }
        return self::$connection;
    }
  }

?>

MySQLService: MySQLService:

<?php

  class MySQLService {
    protected $connectionFactory;

    public function __construct(ConnectionFactory $factory) {
      $this->connectionFactory = $factory;
    }

    public function listItemsForSharerSQL() {
      $conn = $this->connectionFactory->getConnection();
      $items = ...
      return $items;
    }
  }

?>

I call these methods in my controller ItemController : 我在控制器ItemController中调用这些方法:

<?php

require_once ROOT_DIR . "/models/Item.php";
require_once ROOT_DIR . "/ConnectionFactory.php";
require_once ROOT_DIR . "/MySQLService.php";

class ItemController {

    private $data;

    public function listItemsForSharer() {
        $connFactory = new ConnectionFactory();
        $service = new MySQLService($connFactory);
        $items = $service->listItemsForSharerSQL();

        $this->data = ['items' => $items];
        return 'itemslistforsharer-view';
    }
}

?>

I don't like the fact that I've got to instantiate a new ConnectionFactory in my Controller. 我不喜欢必须在Controller中实例化一个新的ConnectionFactory的事实。 Is it a mistake? 错了吗 Should I change my code design? 我应该更改代码设计吗?

It is mistake that you are using a Database in a Controller at all. 您根本在控制器中使用数据库是错误的。 If talking about a Controller from the MVC Architecture pattern, then its Models job to interact with the database. 如果从MVC体系结构模式谈论控制器,那么其模型工作就是与数据库进行交互。 There should be only instances of the Models in the controllers, but not instances of the Database. 控制器中应该只有模型的实例,而数据库应该没有。

Basically in the modern MVC frameworks that Models are extending the base Model class, which has the instantiation of the database. 基本上,在现代MVC框架中,模型正在扩展具有数据库实例化的基础模型类。

As I understand, MySQLService is something like a model since you are returning some items, it seems like you are doing DB operation and want to handle it from a controller. 据我了解,由于要返回一些项目,因此MySQLService类似于模型,似乎您正在执行数据库操作,并希望从控制器处理它。 Instead of every model having its own constructor, you should use a base class. 而不是每个模型都有自己的构造函数,而应使用基类。 I would suggest something like: 我建议类似的东西:

class Model {

    protected function getAdapter() {
        $connection = new ConnectionFactory();
        return $connection->getConnection();
    }

}


class MySQLService extends Model {

    public function listItemsForSharerSQL() {
      $this->getAdapter();
      $items = ...
      return $items;
    }
}

So now your controller will only need 所以现在您的控制器只需要

$service = new MySQLService();

Ofcourse you base class can have the getAdapter() method as a constructor, without any params, so you won't need to pass anything to the MySQLService and won't need to call getAdapther() in each method of MySQLService . 当然,您的基类可以将getAdapter()方法用作构造函数,而无需任何参数,因此您无需将任何内容传递给MySQLService ,也不需要在MySQLService每个方法中调用getAdapther() Depends on your needs. 取决于您的需求。

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

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