哪些数据库模式(ORM,DAO,Active Record等)用于中小型项目? [英] Which database patterns (ORM, DAO, Active Record, etc.) to use for small/medium projects?

查看:154
本文介绍了哪些数据库模式(ORM,DAO,Active Record等)用于中小型项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的房地产网站
具有选择和订购不动产的基本功能。



这是一个小型/简单的项目,但我想写在b


$ b $ p

$ b $ p


你可以建议我使用某种模式来处理数据库?



现在我已经有:

  class db_DBConnection 
{
//这里的基本单例模式...
}

//要扩展的主类其他表DAOs
抽象类db_Table
{
protected $ table;
protected $ order_by;

/ **
*使用准备语句执行指定的查询
*返回可以获取数据的语句对象。
*
* @param $ sql - SQL查询执行
* @param $ params - 通过关联数组将值绑定到标记
* /
protected function executeQuery $ sql,$ params = null)
{
$ dbh = db_DBConnection :: getConnection();
$ stmt = $ dbh-> prepare($ sql);
//将值绑定到标记并执行查询
$ stmt-> execute($ params);
return $ stmt;
}

/ **
* @param id - 从数据库中检索的行的ID
*
*将SQL查询和id发送到executeQuery
*函数返回关联数组,代表
*数据库行。
* /
public function find($ id)
{
$ sql ='SELECT * FROM'。 $ this->表。 'WHERE id =:id LIMIT 1';
// bind id
$ params = array(':id'=> $ id);
//执行并返回关联数组
return $ this-> executeQuery($ sql,$ params) - > fetch(PDO :: FETCH_ASSOC);
}

public function findAll($ quantity,$ where)
{
//返回数组
//表行关联数组:)
// TODO:写这个函数
}

抽象保护函数insert();

抽象保护函数update();

抽象保护函数delete();

// ...


解决方案

p>最好的方法是使用ORM,例如教义。对于小型项目来说,这可能看起来有点太多了,但是从长远来看它是有价值的。



最好使用标准的做事方式,而不是重新发明你自己。



以下是维基百科的 ORMS列表





另外你需要评估你的项目,创建项目自由式可能不是一个很好的主意。其他开发人员将不得不学习您的代码并了解其工作原理等。最好使用熟悉的框架,如 Zend框架 Symfony CakePHP的。您还可以查看可扩展的CMS系统,例如 Joomla Drupal


I writing real estate web site with basic function for choosing and ordering realty.

It is small/simple project, but I want to write it in way, so in future I, or other developers, can turn it into medium business app without rewriting it from scratch.

So what kind of patterns could you advice me to use for dealing with database?

For now I have this:

class db_DBConnection
{
    // basic singleton pattern here...
}

// primary class to be extende by other table DAOs
abstract class db_Table
{
protected $table;
protected $order_by;

/**
 * Executes specified query with prepared statements
 * returns statement object, which can fetch data.
 * 
 * @param $sql - SQL query to execute
 * @param $params - bind values to markers through associative arrays
 */
protected function executeQuery($sql, $params = null)
{
    $dbh = db_DBConnection::getConnection();
    $stmt = $dbh->prepare($sql);
    // binds values to markers and executes query
    $stmt->execute($params);
    return $stmt;
}

/**
 * @param id - id of row to retrieve from database
 *
 * It sends SQL query and id to executeQuery
 * function returns associative array, representing
 * database row.
 */
public function find($id)
{
    $sql = 'SELECT * FROM ' . $this->table . ' WHERE id=:id LIMIT 1';
            // bind id
    $params = array( ':id' => $id );
    // execute and return associative array
    return $this->executeQuery($sql, $params)->fetch(PDO::FETCH_ASSOC);
}

public function findAll($quantity, $where)
{
// Returns array of
// associative arrays of table rows :)
// TODO: write this function
}

abstract protected function insert();

abstract protected function update();

abstract protected function delete();

    // ...

解决方案

The best way would be to use an ORM, like Doctrine. It might seem a little too much for smaller type project, but it pays off in a long run.

It is better to use standard ways of doing things, instead of reinventing your own.

Here is a list of ORMS from Wikipedia.

Also you need to evaluate your project, creating project freestyle might not be a very good idea. Other developers will have to learn your code and understand how it works, etc... It is better to use well know frameworks like Zend Framework, Symfony or CakePHP. You can also look into expandable CMS systems like Joomla and Drupal.

这篇关于哪些数据库模式(ORM,DAO,Active Record等)用于中小型项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆