数据库结果是对象还是数组? [英] Database results as objects or arrays?

查看:103
本文介绍了数据库结果是对象还是数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题类似于 MySQL在PHP中的结果-数组还是对象?但是,我的问题扩展到那里已经讨论过的内容.

This question is similar to Mysql results in PHP - arrays or objects? However, my question expands on what has been discussed there.

我正在尝试确定哪种格式更适合处理数据库结果:对象或数组.我不关心性能(据我了解,它没有什么区别).我的重点还更多地放在显示结果上,而不是创建,更新或删除它们.

I'm trying to decide which format is better for working with database results: objects or arrays. I'm not concerned about performance (from what I understand it makes little difference). My focus is also more on displaying the results—not creating, updating or deleting them.

到目前为止,我一直通过mysqli_fetch_object或PDO的fetchObject之类的函数来使用对象.在我开始进行联接之前,这通常效果很好.连接导致奇怪的对象,这些对象是来自两个或多个表的字段的混合.我的代码很快开始变得混乱.

To date I've always used objects, via functions like mysqli_fetch_object or PDO's fetchObject. This normally works nice, until I start doing joins. Joins lead to strange objects that are a blend of fields from two or more tables. My code quickly starts getting confusing.

我应该注意,我正在分配特定的类名,而不是坚持使用默认的stdClass.我这样做是为了可以访问在类中创建的任何帮助程序方法.例如:

I should note, I'm assigning specific class names, and not sticking with the default stdClass. I do this so that I can access any helper methods I've created in my classes. For example:

foreach ($members as $member)
{
    echo $member->full_name();
    echo $member->age();
}

为清楚起见,我正在考虑将所有数据库结果移至数组.从我读过的文章中,其他人也可以做到这一点.但是,这使我无法轻松访问辅助方法.

For the sake of clarity, I'm considering moving to arrays for all my database results. From what I've read others do this as well. However, this leaves me with no easy way to access my helper methods.

使用上面的示例,我想我可以只输出名字和姓氏,而不用使用full_name()方法,没什么大不了的.至于age()方法,我想我可以创建一个通用实用程序类并将其放入其中.

Using the above example, I guess I could just output both the first and last name instead of using the full_name() method, not a big deal. As for the age() method, I guess I could create a generic utility class and put it in there.

  1. 如果使用(模型)对象,那么如何处理联接?
  2. 如果使用数组,如何处理帮助方法?

推荐答案

我一直使用对象-但我不会直接从查询中放入数据.使用设置"功能,我创建了布局,因此避免了联接和命名冲突的问题.对于您的"full_name"示例,我可能会使用"as"来获取名称部分,在对象中进行设置,然后将"get_full_name"作为成员fn提供.

I've always used objects - but I don't put the data in directly from the query. Using 'set' functions I create the layout and so avoid issues with joins and naming collisions. In the case of your 'full_name' example I would probably use 'as' to get the name parts, set each in the object and offer 'get_full_name' as a member fn.

如果您有雄心壮志,可以将各种东西添加到"get_age"中.一次设置生日,然后从那里开始疯狂.

If you were feeling ambitious you could add all sorts of things to 'get_age'. Set the birth date once and go wild from there.

有几种方法可以使您的数据成为对象.您可以预定义类并创建对象,也可以即时"创建它们.

There are several ways to make objects out of your data. You can predefine the class and create objects or you can create them 'on the fly'.

->一些v简化的示例-如果还不够,我可以添加更多.

--> Some v simplified examples -- if this isn't sufficient I can add more.

即时:

$conn = DBConnection::_getSubjectsDB();  
$query = "select * from studies where Status = 1";  
$st = $conn->prepare( $query );  

$st->execute();  
$rows = $st->fetchAll();  
foreach ( $rows as $row )  
{  
    $study = (object)array();  
    $study->StudyId = $row[ 'StudyId' ];  
    $study->Name = $row[ 'StudyName' ];  
    $study->Investigator = $row[ 'Investigator' ];  
    $study->StartDate = $row[ 'StartDate' ];  
    $study->EndDate = $row[ 'EndDate' ];  
    $study->IRB = $row[ 'IRB' ];  

    array_push( $ret, $study );  
} 

预定义:

/** Single location info
*/
class Location  
{  
    /** Name  
    * @var string  
    */  
    public $Name;  

    /** Address  
    * @var string  
    */  
    public $Address;  

    /** City  
    * @var string  
    */  
    public $City;

    /** State
    * @var string
    */
    public $State;

    /** Zip
    * @var string
    */
    public $Zip;

    /** getMailing
    * Get a 'mailing label' style output
    */
    function getMailing()
    {  
         return $Name . "\n" . $Address . "\n" . $City . "," . $State . "  " . $Zip;
    }
}

用法:

$conn = DBConnection::_getLocationsDB();  
$query = "select * from Locations where Status = 1";  
$st = $conn->prepare( $query );  

$st->execute();  
$rows = $st->fetchAll();  
foreach ( $rows as $row )  
{  
    $location = new Location();  
    $location->Name= $row[ 'Name' ];  
    $location->Address = $row[ 'Address ' ];  
    $location->City = $row[ 'City' ];  
    $location->State = $row[ 'State ' ];  
    $location->Zip = $row[ 'Zip ' ];  

    array_push( $ret, $location );  
} 

然后,您可以在$ ret上循环并输出邮件标签:

Then later you can loop over $ret and output mailing labels:

foreach( $ret as $location )
{ 
    echo $location->getMailing();
}

这篇关于数据库结果是对象还是数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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