Active Record,Code Igniter,加入 table 'users' 两次

Active Record, Code Igniter, join table 'users' twice

我正在努力实现我几年前在 Ruby 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'

希望你能帮助我!

您的多重连接不起作用的原因是您必须给它们别名 (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);
    

希望这对您有所帮助:)