[英]Using DataMapper ORM and Codeigniter to select join table value in a many to many relationship
I have this table structure: 我有这个表结构:
| | users | 用户| --- has many > --- | ---有很多> --- | preferences_users | preferences_users | --- < has many --- | --- <有很多--- | preferences | 偏好|
A preference could be something like "first name" or "surname" but the value for these preferences are stored in the joining table. 首选项可以是“名字”或“姓”之类的东西,但是这些首选项的值存储在联接表中。
I am using Codeigniter and Datamapper ORM to get relational tables into objects, however I am not sure how to get this value in the joining table. 我正在使用Codeigniter和Datamapper ORM将关系表放入对象中,但是我不确定如何在联接表中获取此值。
I am doing this: 我正在这样做:
$user = new User();
$user->where('unique_url', $url)->get();
$user->preferences->get_iterated();
My relationships are set up so that they both have $has_many = array('tablename');
设置我的关系,使它们都具有$has_many = array('tablename');
and I am able to get the values from each table. 而且我能够从每个表中获取值。
HOwever I want to be able to get a table column value from the joining table, does anyone know how to do this? 但是我希望能够从联接表中获取表列值,有人知道如何做到这一点吗?
Thanks, 谢谢,
Ian 伊恩
I found the answer in the documentation : 我在文档中找到了答案:
$object->include_join_fields()
There are no options for this method. 此方法没有选项。 Set it right before adding a relationship. 在添加关系之前将其正确设置。 You can either use it before a
{$query}_related_{$model}
, or before callingget()
on a related item. 您可以在{$query}_related_{$model}
之前使用它,也可以在相关项目上调用get()
之前使用它。 All fields on the table that are not part of the relationship are included, and are prepended with"join_"
. 表中不属于该关系的所有字段都包括在内,并以"join_"
。This method may return unexpected results or throw errors with deep relationships. 此方法可能返回意外结果或引发具有深层关系的错误。
Usage: 用法:
// Create User $u = new User(); $u->get_by_id($userid); // get all alarms for this user, and include the extra 'wasfired' field $u->alarm->include_join_fields()->get(); foreach($u->alarm as $alarm) { if($alarm->join_wasfired) { echo("{$alarm->name} was fired\\n"); } else { echo("{$alarm->name} was NOT fired\\n"); } }
See Working with Join Fields for more details. 有关更多详细信息,请参见使用联接字段 。
You can use this: 您可以使用此:
$Object->joinedObject->include_join_fields()->get();
Then to get the join field: 然后获得join字段:
$Object->{join}_count;
Be aware to add join_ before field (count) name
as referenced in the documentation. 请注意,要在文档中引用的join_ before field (count) name
添加join_ before field (count) name
。
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.