duanmin0941 2010-10-01 13:32
浏览 29
已采纳

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

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

    // ...
  • 写回答

1条回答 默认 最新

  • duanshangying5102 2010-10-01 14:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵