从数据库加载PHP类对象-最佳实践,数据库句柄等 [英] loading a PHP class object from a database--best practices, database handle, etc

查看:76
本文介绍了从数据库加载PHP类对象-最佳实践,数据库句柄等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的问题也许已经在stackoverflow上的其他位置得到了部分回答,但是我觉得通过一起询问可能会得到更全面的答案.我为我可能会键入的任何内容都道歉-我在PHP中工作得不够充分,所以当我这样做时,我觉得我会重新学习一遍.

I think perhaps my questions have been answered in part in other locations on stackoverflow, but I felt I might get a more comprehensive answer by asking it all together. I apologize for anything dumb I might type--I don't work in PHP nearly enough, so when I do, I feel like I'm learning it all over again.

无论如何,假设我有一个名为"items"的mysql数据库表,其中包含以下列:item_iditem_typeitem_name.我有一个PHP类,如下所示:

Anyway, let's say I've got a mysql database table called "items" with the following columns: item_id, item_type, item_name. And I've got a PHP class that looks like this:

class Item
{
    public $item_id;
    public $item_type;
    public $item_name;
}

换句话说,我真的只想要从数据库到类对象的直接映射.

In other words, I really just want a direct map from the database to the class object.

我决定在名为"load"的类中包含一个方法,该方法在提供item_id时检索项目数据.所以这是我的两个问题:

I've decided to include a method in the class called "load" that retrieves the item data when supplied with the item_id. So here are my two questions:

  1. 在类中连接数据库的最佳方法是什么?我应该在类内部实例化一个新的PDO对象,还是应该将PDO数据库句柄作为load方法的参数传递进来?有关系吗?
  2. 这是否是分配类属性的一种方式,或者有一种更聪明的方式来做到这一点:

  1. What is the best way to connect to the database within the class? Should I be instantiating a new PDO object inside the class, or should I be passing the PDO database handle in as a parameter on the load method? Does it matter?
  2. Does this make any sense as a way to assign the class properties, or is there a much smarter way to do this:

$sth = $dbh->query("SELECT * FROM items WHERE item_id = $item_id");
$sth->setFetchMode(PDO::FETCH_CLASS, 'Item');
while ($obj = $sth->fetch()) {
    foreach ($obj as $key => $value) {
        $this->$key = $value;
    }
}   

修改: 看到这些回答(可以肯定会引起有趣的阅读)后,我意识到我可能没有进行足够的详细介绍.

I realized after seeing these responses (which make for interesting reading, to be sure), that I probably didn't go into enough detail.

我认为使用ORM库可能对我正在做的事情有些大材小用(只是个人爱好项目,对雇主来说不算什么),但实际上,无论如何,我不确定那是我真正需要的.这就是为什么.因为我可能会有这样的东西:

I think using an ORM library might be overkill for what I'm doing (just a personal hobby project--not something for an employer), but actually, I'm not certain that's quite what I need, anyway. Here's why. Because I might have something like this:

class InventoryItem extends Item
{
    public $user_id;
    public $quantity;
}

我还没有真正考虑到这一点.仅举一个例子.因此,我用来获取数据的查询可能看起来像这样:

I haven't really thought this through. Just giving an example. So the query I'd use to get the data might look something like this:

SELECT * 
FROM items, inventories 
WHERE item.item_id = inventories.item_id 
AND user_id = $user_id;

换句话说,我必须创建一个可以正确提取该类数据的SQL语句,而我真的只需要知道是否应该传递数据库句柄或在load方法中创建它即可. ,以及上面的代码是否可以作为分配属性值的好方法.

In other words, I'd have to create the SQL statement that would correctly pull out the data for the class, and I really just need to know if I should be passing in my database handle or creating it inside the load method, and if the code above makes sense as a good way to assign the property values.

另一个麻烦是,我可能必须操纵某些数据(例如,如果其中一列正在存储json编码的数组或其他内容).

An additional wrinkle is that I might have to manipulate some of the data (e.g., if maybe one of the columns is storing a json encoded array or something).

推荐答案

我使用的是通用的数据库对象类,因此我有单独的类(与数据库中的不同表匹配),每个类都对此进行了扩展.

I use a database object class that is generic, then I have individual classes (which match the different tables in the database) which each extend that.

数据库对象类包含用于将行实例化为对象并执行任何操作的函数,扩展它们的类仅包含每个表对象的属性.

The database object class contains the functions to instantiate the rows into objects, and perform any crud, the classes that extend them just contain the attributes of each table object.

最好在程序员.stackexchange上问这个问题,因为它是一个开放式的问题,我想有很多不同的方法可以做到这一点并就此事发表意见.

This might be better asked over on programmers.stackexchange, as its quite an open ended question, and I imagine there are many different ways to do this and opinions on the matter.

这篇关于从数据库加载PHP类对象-最佳实践,数据库句柄等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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