提问者:小点点

活动记录、代码点火器、联接表“用户”两次


我正在努力实现几年前在RubyonRails中所做的事情<现在我正在使用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个答案

匿名用户

多重联接不起作用的原因是您必须为它们提供别名(源)。一旦得到结果,就需要对其进行迭代,以便能够按照您希望的方式构建对象。

>

  • 步骤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);
    }
    

    第三步:检查你的结果

    var_dump($results);
    

    希望这有助于:)