连接在Codeigniter活动记录中返回的自定义对象 [英] Custom object returned by join in Codeigniter Active record

查看:75
本文介绍了连接在Codeigniter活动记录中返回的自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个数据库表:

I have these two database tables:

位置
id
名称

locations
id
name

用户
id
location_id
last_name
first_name

users
id
location_id
last_name
first_name

我也有 User Location 类,它们都扩展了 Model 并包含一些自定义方法.例如,在 User 类中有一个 get_full_name()方法.

I also have User and Location class, they both extends Model and contain some custom methods. For example, there is a get_full_name() method in the User class.

我正在使用以下代码加载数据,

I am using the following codes to load the data,

$this->db->select('users.id, locations.name as location_name, users.last_name, users.first_name');
$this->db->from('users');
$this->db->join('locations', 'users.location_id = location.id');
$query = $this->db->get();
$users= $query->custom_result_object('User'); //now $users is an array of User object

custom_result_object 是Codeigniter中的内置但未记录的功能.它接受一个类名字符串,并将用于创建该类的对象.

The custom_result_object is a built-in but undocumented function in Codeigniter. It accepts a string of class name and will use it to create objects of that class.

使用上面的代码,我可以像这样访问数据,

With the above codes, I can access the data like this,

foreach($users as $user)
{
 echo $user->id;
 echo $user->get_full_name();
 echo $user->location_name;
}

但是正如您所看到的,location_name现在是 User 对象的字段,我想要的是 User 对象具有 Location 的字段strong>对象,允许我这样使用它,

But as you can see, location_name is now a field of the User object, what I want is the User object has a field of Location object that allows me to use it like this,

foreach($users as $user)
{
 echo $user->id;
 echo $user->get_full_name();
 echo $user->location->name; // Location is an object
}

我不想使用DataMapper ORM或任何其他ORM,并且还希望避免出现N + 1查询性能问题.

I don't want to use DataMapper ORM or any other ORMs, and would also like to avoid the N+1 query performance issue.

如何用最少的代码做到这一点?

How can I do this with minimal amount of codes?

注意:我仅出于演示目的组成了此示例,在我的实际情况下,有很多表和类(已在其上定义了自定义方法).

Note: I made up this example just for the demonstration purpose, in my real case, there are quite a lot of tables and classes (which has custom methods defined on them).

非常感谢大家.

推荐答案

我对自己也提出了同样的疑问,但是发现最简单的方法是遍历每个结果(在您的情况下是用户)并选择相应的子对象(位置)并设置为父对象(用户->位置)中的字段.

I had questioned the same for myself but found the simplest way was to iterate through each result (in your case, user) and select the corresponding child (location) and set to the field in the parent object (user->location).

我能想到的唯一替代方法是编辑(或扩展和创建自己的数据库集)db_result类,以使用getters/setters代替直接字段-也许使用call_user_func_array().此时,您可以在用户模型中编写setId()函数,以在收到userId/外键后设置$ location字段.

The only alternative way I can think of would involve editing (or extending and creating your own db set) the db_result class to use getters/setters instead of direct fields - perhaps with call_user_func_array(). At that point, you could code your setId() function in the user model to set the $location field upon receiving the userId/foreign key.

这篇关于连接在Codeigniter活动记录中返回的自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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