codeigniter-base-model
codeigniter-base-model copied to clipboard
Unable to create relations to self/static, nesting issue
I've tried relating a model to itself by having parent => child relations, for example:
- Parent Category has many Child Categories
- Child Categories belongs to a Parent Category
A category is a category, not a parent_category or child_category, or they shouldn't be. Below is my relation within my category_model:
public $belongs_to = array( 'parent' => array( 'model' => 'category_model', 'primary_key' => 'parent_category' ), );
The following errors are thrown:
Message: Undefined index: parent_category
Fatal error: Maximum function nesting level of '100' reached, aborting!
Why it can't find the parent_category field I can't understand, the nesting issue would be down to it being stuck in a loop.
What's the best way of achieving this without creating a new model and extending the base (Category_Model / MY_Model) model?
Although an old issue it may be useful for others, I have a similar setup, using a one:many relationship though ($has_many) and only selecting the records where parent is null - this way a recursive tree can be built. It's a bit messy wih foreach loops, but it works none the less for the moment.
I had to edit the base model and remove
$this->_with = array();
from the get_all() function, around about line 231.
My tree now fully generates.
I'm working with a small-ish sized dataset of categories (about 500), and a few thousand products, and haven't had any issues with get_all(), unless there is another problem I've missed.
Hope this helps!