markerpower
markerpower

Reputation: 345

Kohana ORM has_many belongs_to

Lets say I have 3 models, One, Two, and Three.

Model One has many Three, Model Two has many Three, Three belongs to One and Two.

Models:

class Model_One extends ORM {

protected $_primary_key = 'one_id';

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

class Model_Two extends ORM {

protected $_primary_key = 'two_id';

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

class Model_Three extends ORM {

protected $_primary_key = 'three_id';

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

'twos'=> array(
        'model' => 'two',                
        'through' => 'twos_threes',    
        'far_key' => 'two_id',       
        'foreign_key' => 'three_id'   
        ),
);
}

I display One, Two, Three.

$getTwos=ORM::factory('two')->find_all(); 

foreach($getTwos as $getTwo)
{
   echo $getTwo->two_category_title;
   foreach($getTwo->three->find_all() as $getThree)
   {
       echo $getThree->three_title;
       echo $getThree->one->one_author; 
   }
}

Lets say I have author A and B and Title 1, Title 2, Title 3, and Title 4. A has Title 1, 2, 3, and B has Title 4.

Problem is echo $getThree->one->one_author; will echo A, B, NULL, NULL.

How can I echo the information correctly?

Upvotes: 0

Views: 4825

Answers (1)

biakaveron
biakaveron

Reputation: 5483

You have incorrect relation definitions in your model Three. It seems like One has and belongs to many Threes (HABTM or "has many through"), the same for Two model. Here is what you need:

class Model_Three extends ORM {

protected $_primary_key = 'three_id';

protected $_has_many = array(
    'ones'=> array(
        'model' => 'one',                
        'through' => 'ones_threes',    
        'far_key' => 'one_id',       
        'foreign_key' => 'three_id'   
        ),

    'twos'=> array(
        'model' => 'two',                
        'through' => 'twos_threes',    
        'far_key' => 'two_id',       
        'foreign_key' => 'three_id'   
        ),
    );
}

PS. foreign_key is optional, because you already define it in $_primary_key property.

PPS. Here is an example for a "Post belongs to only one User" relation:

class Model_User extends ORM {

   protected $_has_many = array(
      'posts' => array(),
   );
}

class Model_Post extends ORM {

   protected $_belongs_to = array(
      'author' => array(
          'model'       => 'user',
          // ignore if you have a `author_id` foreign key
          'foreign_key' => 'user_id', 
      ),
   );

   protected $_has_many = array(...);
}

// usage
$post = ORM::factory('post', 1);
echo $post->author->username;
$post->author = ORM::factory('user', 1);
$user = ORM::factory('user', 1);
foreach($user->posts->where('published', '=', 1)->find_all() as $posts) {
    echo $post->title;
}

Upvotes: 1

Related Questions