哪些数据库模式(ORM、DAO、Active Record 等)可用于中小型项目?

发布于 2024-09-26 01:29:33 字数 1553 浏览 1 评论 0 原文

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

这是一个小/简单的项目,但我想用这样的方式写它, 所以将来我或其他开发人员可以将其变成中型商业应用程序而无需重写它 从头开始。

那么您可以建议我使用什么样的模式来处理数据库?

现在我有这个:

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();

    // ...

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();

    // ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2024-10-03 01:29:33

最好的方法是使用 ORM,例如 Doctrine。对于小型项目来说,这似乎有点太多了,但从长远来看,这是值得的。

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

以下是来自 Wikipedia 的 ORMS 列表

您还需要评估您的项目,自由式创建项目可能不是一个好主意。其他开发人员必须学习您的代码并了解它是如何工作的等...最好使用众所周知的框架,例如 Zend框架SymfonyCakePHP。您还可以研究可扩展的 CMS 系统,例如 JoomlaDrupal

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文