douji2283 2014-03-31 13:50
浏览 27
已采纳

在类中使用数据库

I´m wondering how to work with database in classes. For example how to write queries for easy future edit and by OOP rules.

First example:

    class User {

    public function select($query) {
        //work with data
        return $data;
    }

    public function insert($query) {
        //work with data
    }
}

In this example user (me) add queries for each method. For me, big disadvantage is that I must remember correct form of my query. This example is easy to understand, but in case I will work with more complex queries, it would hard to write correct form of query.

Next example:

class User {

    const select = "SELECT * FROM user WHERE ID = ?";
    const insert = "INSERT INTO user VALUES (?)";

    public function select($id) {
        //work with data
        return $data;
    }

    public function insert($id) {
        //work with data
    }
}

I defined each query as contstant. This form of class is very handy but on the other hand if I want to get ID of user I can't, because I would know name of ID field in the database. This problem I solve in third example

Third example:

class User {

    private $table, $idField;

    public function setDatabaseInformation($table, $idField) {
        $this->table = $table;
        $this->idField = $idField;
    }

    public function select($id) {
        //work with data
        //query as SELECT * FROM $this->table WHERE $this->idField = ?
        return $data;
    }

    public function insert($id) {
        //query as INSERT INTO $this->table VALUES (?)";
        //work with data
    }

}

Personaly I think, this example is the best, because there I have everything what I want and I can't rememeber forms of queries. On the other hand, this example is a bit longer, very universal and I don't know it is correct by OOP rules.

My questions are: Is there any other options how to write queries in database? Is the last example correct by OOP rules? Which way do you use?

Thanks for suggestions.

  • 写回答

1条回答 默认 最新

  • dsbiw2911188 2014-03-31 14:01
    关注

    Abstract the whole thing inside the class! You want to write code which looks like this in the end:

    $database    = new PDO(...); // or whatever
    $userStorage = new UserStorage($database);
    
    $user  = $userStorage->getById(42);
    $users = $userStorage->getByName('Joe');
    
    class UserStorage {
    
        protected $db;
    
        public function __construct(PDO $db) {
            $this->db = $db;
        }
    
        public function getById($id) {
            $stmt = $this->db->prepare('SELECT ... WHERE id = ?');
            $stmt->execute([$id]);
            ...
            return $user;
        }
    
        ...
    
    }
    

    You only want to write each logical query once in one specific, authoritative place. For each possible logical "action", create a method (one method to get users by id, one to get them by name, etc.). Each method can be as complex or simple as necessary internally, but it is the one place where you write the specific query to get the specific data. Lastly: dependency inject your database connection.

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

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体