Active Record,Code Igniter,连接表'用户'两次

时间:2015-08-04 15:33:18

标签: php codeigniter activerecord query-builder codeigniter-3

我正在努力实现几年前我在Ruby on Rails上所做的事情 现在我正在使用CodeIgniter 3.0.0 下面概述了我想要实现的目标:

表:

Table: users               Table: articles
_________________          _______________________________________
| id | name     |          | id | description | buyer | applicant |
|  1 | John D.  |          |  1 | PC Mouse    |     1 |         2 |
|  2 | Anita F. |          

我想实现这个目标:(或类似的东西)

        Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [description] => 'PC Mouse'
            [buyer] => stdClass Object
                (
                     [id] => 1
                     [name] => 'John D.'
                )
            [applicant] => stdClass Object
                (
                     [id] => 2
                     [name] => 'Anita F.'
                )
        )
)

所以我可以打电话:

echo $article->description;       //=> PC Mouse
echo $article->buyer->name;       //=> John D.
echo $article->applicant->name;   //=> Anita F.

<小时/>

我尝试过(没有运气):

$this->db->from('articles');
$this->db->join('users', 'articles.buyer = users.id');
$this->db->join('users'. 'articles.applicant = users.id');
$objects = $this->db->get()->result_object();

//=> Throws:
Error 1066:
Not unique table/alias: 'users'
$this->db->from('articles');
$this->db->join('users', 'articles.buyer = users.id');
$objects = $this->db->get()->result_object();

//=> returns an extra column 'name' with the value 'John D.'
        Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [description] => 'PC Mouse'
            [buyer] => 1
            [applicant] => 2
            [name] => 'John D.'
         )
)

我读了很多关于group_by的内容,但我无法让它发挥作用 你能帮我搞清楚吗? 这甚至可能是一个解决方案,甚至可能吗?!

侧面信息

这是我需要的一个精简的例子。 我有更多的联接。

这是我最终应该拥有的:

purchases has many articles        (articles.purchasesID)
articles  has one  status          (statusID)
articles  has one  brand           (brandID)
purchases has one  buyer           (usersID)
purchases has one  applicant       (usersID)
purchases has one  status          (statusID)
purchases has one  delivered_to    (usersID)
purchases has one  suppliers       (suppliersID)
purchases has one  concerns        (concernsID

// When I want to echo the status of an article:
// Status has an 'id' and 'name' so:
echo $purchase->article[0]->status->name  //=> 'Processing'

// or when I want to know who the product is delivered to:
echo $purchase->delivered_to->name  //=> 'Carsten'

希望你能帮助我!

1 个答案:

答案 0 :(得分:1)

您的多个联接无法正常工作的原因是您必须为其提供别名source)。获得结果后,您需要遍历它们,以便您可以按照自己的方式构建对象。

  • 第1步:构建您的查询

    $this->db->select('
        a.*,
        bu.name as buyer_name,
        au.name as applicant_name
    ');
    $this->db->from('articles a');
    $this->db->join('users bu', 'a.buyer = bu.id', 'left');
    $this->db->join('users au', 'a.applicant = au.id', 'left');
    $results = $this->db->get()->result();
    
  • 第2步:遍历您的搜索结果以构建您想要的对象

    foreach ($results as $res) {
        $buyer_obj = new stdClass();
        $buyer_obj->id = $res->buyer;
        $buyer_obj->name = $res->buyer_name;
        $res->buyer = $buyer_obj;
        unset($res->buyer_name);
    
        $applicant_obj = new stdClass();
        $applicant_obj->id = $res->applicant;
        $applicant_obj->name = $res->applicant_name;
        $res->applicant = $applicant_obj;
        unset($res->applicant_name);
    }
    
  • 第3步:检查结果

    var_dump($results);
    

希望这会有所帮助:)