0

我不知道如何处理这个问题。

假设我有 3 个模型,A、B 和 C。

A型有很多C,B型有很多C,C属于A和B。

我得到了所有的B。

$getBs=ORM::factory('B')->find_all(); 

我显示A,B,C。

foreach($getBs as $getB)
{
    echo $getB->b_category_title;
    foreach($getB->C->find_all() as $getC)
    {
        echo $getC->c_title;
        echo $getA->a_author; //Problem part
    }
}

在显示 Model C 的信息时,我不知道如何访问 Model A 并将其连接到 Model C。

编辑

为了获得工作代码,我将模型 A - C 更改为模型一 - 三。

使用 _load_with 的 biakaveron 示例,我收到以下错误:

Database_Exception [ 1054 ]: Unknown column 'three.id_c' in 'on clause' [ SELECT `ones`.`a_id` AS `ones:a_id`, `ones`.`a_author` AS `ones:a_author`, `three`.* FROM `threes` AS `three` JOIN `twos_threes` ON (`twos_threes`.`id_c` = `three`.`c_id`) LEFT JOIN `ones` AS `ones` ON (`ones`.`a_id` = `three`.`id_c`) WHERE `twos_threes`.`id_b` = '1' ]

楷模:

class Model_One extends ORM {

protected $_primary_key = 'a_id';

protected $_has_many = array(
    'threes'=> array(
        'model' => 'three',                
        'through' => 'ones_threes',   
        'far_key' => 'id_c',       
        'foreign_key' => 'id_a'   
        ),
    );
}

class Model_Two extends ORM {

protected $_primary_key = 'b_id';

protected $_has_many = array(
    'threes'=> array(
        'model' => 'three',                
        'through' => 'twos_threes',   
        'far_key' => 'id_c',       
        'foreign_key' => 'id_b'   
        ),
    );
}

class Model_Three extends ORM {

protected $_primary_key = 'c_id';

protected $_belongs_to = array(
    'ones'=> array(
        'model' => 'one',                
        'through' => 'ones_threes',    
        'far_key' => 'id_a',       
        'foreign_key' => 'id_c'   
        ),

'twos'=> array(
        'model' => 'two',                
        'through' => 'twos_threes',    
        'far_key' => 'id_b',       
        'foreign_key' => 'id_c'   
        ),
);

protected $_load_with = array('ones');
}

为什么要寻找three.id_c?

4

1 回答 1

2

C 属于 A 和 B。

foreach($getBs as $getB)
{
    echo $getB->b_category_title;
    foreach($getB->C->find_all() as $getC)
    {
        echo $getC->c_title;
        echo $getC->A->a_author; 
    }
}

PS。只是一个注释。$_load_with您可以使用属性加载 C 和 A 对象:

class Model_C extends ORM {
    // ...
    protected $_load_with = array('A');
    // ...
}
于 2012-05-17T06:29:12.197 回答