markerpower
markerpower

Reputation: 345

Kohana ORM Relationships and Displaying Information

I don't know how to approach this.

Lets say I have 3 models, A, B, and C.

Model A has many C, Model B has many C, C belongs to A and B.

I get all of B.

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

I display 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
    }
}

I do not know how to access and connect Model A to Model C when displaying information for Model C.

Edit

To get working code, I change Model A - C to Model One - Three.

Using biakaveron example of _load_with, I get the following error:

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' ]

Models:

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');
}

Why is it looking for three.id_c?

Upvotes: 0

Views: 527

Answers (1)

biakaveron
biakaveron

Reputation: 5483

C belongs to A and 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. Just a note. You can load both C and A objects using $_load_with property:

class Model_C extends ORM {
    // ...
    protected $_load_with = array('A');
    // ...
}

Upvotes: 2

Related Questions