【问题标题】:Using DataMapper ORM and Codeigniter to select join table value in a many to many relationship使用 DataMapper ORM 和 Codeigniter 在多对多关系中选择连接表值
【发布时间】:2012-09-02 05:36:42
【问题描述】:

我有这个表结构:

|用户 | --- 有很多 > --- |偏好_用户 | ---

首选项可以是“名字”或“姓氏”,但这些首选项的值存储在连接表中。

我正在使用 Codeigniter 和 Datamapper ORM 将关系表转换为对象,但是我不确定如何在连接表中获取此值。

我正在这样做:

$user = new User();
$user->where('unique_url', $url)->get();
$user->preferences->get_iterated();

我的关系已设置为它们都有$has_many = array('tablename');,并且我能够从每个表中获取值。

如果我希望能够从连接表中获取表列值,有人知道该怎么做吗?

谢谢,

伊恩

【问题讨论】:

    标签: php codeigniter orm datamapper codeigniter-datamapper


    【解决方案1】:

    我找到了答案in the documentation

    $object->include_join_fields()

    此方法没有选项。在添加之前设置它 关系。您可以在之前使用它 {$query}_related_{$model},或在相关项目上调用 get() 之前。 表上不属于关系的所有字段都是 包括在内,并在前面加上"join_"

    此方法可能会返回意外结果或抛出深度错误 关系。

    用法:

    // 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");
        }
    }
    

    更多详情请见Working with Join Fields

    【讨论】:

      【解决方案2】:

      你可以用这个:

      $Object->joinedObject->include_join_fields()->get();
      

      然后获取join字段:

      $Object->{join}_count;
      

      请注意添加join_ before field (count) name,如文档中引用的那样。

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-28
        • 1970-01-01
        • 2014-12-24
        相关资源
        最近更新 更多