CodeIgniter Forums
CI do not have model to model relationships as like Yii. Plz implement if possible. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: CI do not have model to model relationships as like Yii. Plz implement if possible. (/showthread.php?tid=69559)



CI do not have model to model relationships as like Yii. Plz implement if possible. - bawa_d - 12-14-2017

CI do not have model to model relationships as like Yii. Plz implement if possible.

example u have table_one and want to join table_two using their id

Code:
$this->db->select('columns');
$this->db->from('table_one');
$this->db->join('table_two', 'table_two.id = table_one.id');


I know with yii, you can do it this way:
Code:
$posts=Post::model()->with(
'author.profile',
'author.posts',
'categories')->findAll();


this feature should be integrated in CI4. The benefit for implementing this is user ended up with optimized query.


RE: CI do not have model to model relationships as like Yii. Plz implement if possible. - kilishan - 12-14-2017

Is it really a more optimized query? Behind the scenes, the with() command is still making joins, it's just making assumptions about what the fields on each table to join are. And, while it's true that a majority of cases will comply with that, it becomes just as verbose if you have special requirements that don't conform to the standard.

I'm not familiar enough with Yii to know, but that sounds like that call is part of an ORM system, which can make efficient queries when getting related fields, but it's not the same thing as a simple join.

CodeIgniter has always been a framework that leans more to the explicit side of code than the implicit, and this isn't expected to change much in the future.


RE: CI do not have model to model relationships as like Yii. Plz implement if possible. - EpicKris - 12-21-2017

This thread may have some helpful answers: https://forum.codeigniter.com/thread-67594.html


RE: CI do not have model to model relationships as like Yii. Plz implement if possible. - natanfelles - 01-16-2018

If relationships are added in the Model, I believe that the configuration style of the Fuel properties are very interesting.

PHP Code:
// in a Model_Post which has many comments
protected static $_has_many = array(
 
   'comments' => array(
 
       'key_from' => 'id',
 
       'model_to' => 'Model_Comment',
 
       'key_to' => 'post_id',
 
       'cascade_save' => true,
 
       'cascade_delete' => false,
 
   )
);