codeigniter-base-model icon indicating copy to clipboard operation
codeigniter-base-model copied to clipboard

$has_many and get_all() returns relationship for first value only.

Open D-Bush opened this issue 12 years ago • 3 comments

In function relate(){} just before return $row; you clear the $this->_with variable by setting it back to an empty array. This causes the relationship to only fire on the first row returned, after which if(in_array($relationship, $this->_with){} will not fire. Removing $this->_with = array(); from the end of the function causes it to fire properly on a $has_many relationship.

My test case is:

class Image_model extends MY_Model(){

    public $has_many = array( "image_description" );

}

class Image_description_model extends MY_Model {

    public $belongs_to = array( "image" );

}

And inside the controller:

$data['images'] = $this->image_model->with('image_description')->limit( 50, 0 )->get_all();

D-Bush avatar Sep 20 '12 19:09 D-Bush

@D-Bush thank you for pointing this out. I just ran into this exact issue, and another in fact that I posted a new issue for... but you helped me resolve this problem quickly, thanks!

mikerogne avatar Sep 30 '12 01:09 mikerogne

Ah. A very good point. has_many relationships are something I hadn't actually used myself yet so never noticed it. I'll get that fixed soon (unless someone else wants to?). Thanks for letting me know!

jamierumbelow avatar Sep 30 '12 10:09 jamierumbelow

I was about to post this very same issue.

JoseTomasTocino avatar Oct 21 '12 19:10 JoseTomasTocino