我应该在模型中设置db对象的位置? (MVC)

时间:2023-01-05 04:17:48

I would like to know where is the best place to set my db object with my model.

我想知道用我的模型设置db对象的最佳位置在哪里。

Should I hard coded it since my model should be designed for one project, so i set it inside my constructor or wherever i do initialization ? or Should I pass my db object to my constructor when instancing my object ?

我应该硬编码它,因为我的模型应该是为一个项目设计的,所以我将它设置在我的构造函数内或无论我在哪里进行初始化?或者,在实例化对象时,是否应该将db对象传递给构造函数?

What is the best way, i mean from experimented users, and efficient that'll give me more confort to use ?

什么是最好的方式,我的意思是来自实验用户,高效,这将给我更多的使用舒适?

3 个解决方案

#1


Couple of things:

几件事:

  1. Most PHP projects that utilize a database connection represent that database using a Singleton pattern, if you aren't sure what this is, read up on it.

    大多数利用数据库连接的PHP项目使用Singleton模式表示该数据库,如果您不确定这是什么,请阅读它。

  2. Typically I define my database connections in a configuration file which can easily be changed between environments (development, stage, production).

    通常,我在配置文件中定义数据库连接,可以在环境(开发,阶段,生产)之间轻松更改。

  3. I'll then instantiate my database connection in a bootstrap file using the aforementioned Singleton pattern and configuration file.

    然后,我将使用前面提到的Singleton模式和配置文件在引导程序文件中实例化我的数据库连接。

  4. My models will typically completely abstract the database/table data store, for each model I'll do something like this:

    我的模型通常会完全抽象数据库/表数据存储,对于每个模型,我会做这样的事情:

bootstrap.php

$config = load_config_data(ENVIRONMENT);

Db::setDefaultAdapter($config['database']);

Model/Table/User.php

class Table_User extends Db_Table 
{
  // Table name
  protected $_name = 'user';

  /* Do a bunch of database specific stuff */
}

Model/User.php

class User extends Model 
{
  public function updateUsername($userid, $username)
  {
    // Uses default adapter, Singleton pattern!
    $table = Db::loadTable('user');
    $table->update(
      array('username'=>$username),
      Db::quoteInto('userid = ?', $userid)
    );
  }
}

This is pretty much an introduction to the Model in the Zend Framework MVC, I would check it out for some ideas on how to organize your code (or save yourself some trouble and actually use the framework.)

这几乎是对Zend Framework MVC中模型的介绍,我会检查一下如何组织代码(或者省去一些麻烦并实际使用框架)。

#2


For testability, you should pass it into the constructor rather than hard coding it. This helps you to write unit test because you can mock your DB object.

为了测试性,您应该将其传递给构造函数而不是对其进行硬编码。这有助于您编写单元测试,因为您可以模拟数据库对象。

#3


I would not hard code it, even if the code is never used for another project simply moving from a test database to a live database may require locating and changing the code in the model class. That would be far better placed in some kind of configuration file.

我不会硬编码,即使代码从未用于另一个项目,只是从测试数据库移动到实时数据库可能需要定位和更改模型类中的代码。在某种配置文件中,这将更好。

Personally, I would have the db object defined in whatever you use as a bootstrap - and then have the model(s) use that single object.

就个人而言,我会在您用作引导程序的任何内容中定义db对象 - 然后让模型使用该单个对象。

#1


Couple of things:

几件事:

  1. Most PHP projects that utilize a database connection represent that database using a Singleton pattern, if you aren't sure what this is, read up on it.

    大多数利用数据库连接的PHP项目使用Singleton模式表示该数据库,如果您不确定这是什么,请阅读它。

  2. Typically I define my database connections in a configuration file which can easily be changed between environments (development, stage, production).

    通常,我在配置文件中定义数据库连接,可以在环境(开发,阶段,生产)之间轻松更改。

  3. I'll then instantiate my database connection in a bootstrap file using the aforementioned Singleton pattern and configuration file.

    然后,我将使用前面提到的Singleton模式和配置文件在引导程序文件中实例化我的数据库连接。

  4. My models will typically completely abstract the database/table data store, for each model I'll do something like this:

    我的模型通常会完全抽象数据库/表数据存储,对于每个模型,我会做这样的事情:

bootstrap.php

$config = load_config_data(ENVIRONMENT);

Db::setDefaultAdapter($config['database']);

Model/Table/User.php

class Table_User extends Db_Table 
{
  // Table name
  protected $_name = 'user';

  /* Do a bunch of database specific stuff */
}

Model/User.php

class User extends Model 
{
  public function updateUsername($userid, $username)
  {
    // Uses default adapter, Singleton pattern!
    $table = Db::loadTable('user');
    $table->update(
      array('username'=>$username),
      Db::quoteInto('userid = ?', $userid)
    );
  }
}

This is pretty much an introduction to the Model in the Zend Framework MVC, I would check it out for some ideas on how to organize your code (or save yourself some trouble and actually use the framework.)

这几乎是对Zend Framework MVC中模型的介绍,我会检查一下如何组织代码(或者省去一些麻烦并实际使用框架)。

#2


For testability, you should pass it into the constructor rather than hard coding it. This helps you to write unit test because you can mock your DB object.

为了测试性,您应该将其传递给构造函数而不是对其进行硬编码。这有助于您编写单元测试,因为您可以模拟数据库对象。

#3


I would not hard code it, even if the code is never used for another project simply moving from a test database to a live database may require locating and changing the code in the model class. That would be far better placed in some kind of configuration file.

我不会硬编码,即使代码从未用于另一个项目,只是从测试数据库移动到实时数据库可能需要定位和更改模型类中的代码。在某种配置文件中,这将更好。

Personally, I would have the db object defined in whatever you use as a bootstrap - and then have the model(s) use that single object.

就个人而言,我会在您用作引导程序的任何内容中定义db对象 - 然后让模型使用该单个对象。