CodeIgniter Forums

Full Version: Datamapper deep relationships
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]bralens[/eluser]
Hello, i have problem with deep relations.


My post model
Code:
var $has_one = array(
  'parent' => array(
   'class'         => 'post',
   'other_field'   => 'childrens',
   'join_other_as' => 'childrens',
   'join_self_as'  => 'parent',
   'join_table'    => 'relationships'
  )
);
var $has_many = array(
  'childrens' => array(
   'class'         => 'post',
   'other_field'   => 'parent',
   'join_other_as' => 'parent',
   'join_self_as'  => 'childrens',
   'join_table'    => 'relationships'
  )
);

And i need to get all brothers and sisters (parent/childrens), but when i do
Code:
$this
   ->where_related('parent/childrens', 'id', $related_id)
   ->where_not_in('id', $related_id)
   ->group_by('id')
   ->order_by('title')
   ->get();

i get this error
Code:
Cannot use object of type Post as array in datamapper/libraries/Datamapper.php on line 2739

odd, but
Code:
$this
   ->where_related('childrens/parent', 'id', $related_id)
   ->where_not_in('id', $related_id)
   ->group_by('id')
   ->order_by('title')
   ->get();
don't make error and seems to be doing what i need...
feels like datamaper works backwards...

El Forum

[eluser]WanWizard[/eluser]
I can't comment on that.

This shows a model called "Post" which has a "has_one" to "parent" and a "has_many" to "childrens". It doesn't show the relations between "parent" and "childrens", which is what is used when you specify "parent/childrens".

Second point is that in a relationship definition, "class" points the the model class the relationship is pointing to. Which in this example is the "Post" model itself?

El Forum

[eluser]bralens[/eluser]
Ok, i forgot to mention that it is self-relationship and in future i will need alot of that kind of self-relationships.

First i throught of join fields (but there will be multiple differnet self-relationships with the same posts and i don't think that join fields is best for that)

Than i found "Multiple Relationships to the Same Model" in datamapper docs.
There is example with creator and editor.

I need that but to be self relationship.

So what you recommend me to do and how?

El Forum

[eluser]WanWizard[/eluser]
Self relationships are documented under "Advanced relationships".

The example is many-to-many, but one-to-many is defined in exactly the same way, but then without the relationship table. Take care how you name the FK (the join_self and join_other values).