2

I am looking at how to create self-relationship (upsteam) join in ORM Datamaper. my self - relation class looks:

<?php
class Prospect extends DataMapper{

    var $has_one =array(
        'parent' => array(
            'class' => 'prospect',
            'other_field'=>'prospect'
        ),
        'prospect' => array(
        'other_field' => 'parent'
        )
    );




}

and I try to list

include_related (parent)
$p->include_related('parent')->get();

I have had error Fatal error: Cannot use object of type Prospect as array in [..]application/libraries/Datamapper.php on line 2739

I can do

$p->include_related('prospect')->get();

but in this case I have had a wrong join.

SELECT `prospects`.*, `prospect_prospects`.`id` AS prospect_id, `prospect_prospects`.`name` LEFT OUTER JOIN `prospects` prospect_prospects ON `prospects`.`id` = `prospect_prospects`.`parent_id`

Results shows relation parent -> child (downstream relation) not child-> parent (upstream relation) I am lookig for:

SELECT `prospects`.*, `prospect_prospects`.`id` AS prospect_id, `prospect_prospects`.`name` LEFT OUTER JOIN `prospects` prospect_prospects ON `prospects`.`parent_id` = `prospect_prospects`.`id`

Any tips how to do it, and what I have to change it ? and how should look a good upstream relationship class / php code ?

ps: direct SQL query works brilliant.

Charan Pai
  • 2,288
  • 5
  • 32
  • 44
Pawel
  • 21
  • 3

2 Answers2

0

I think what you're dealing with is a "Reciprocal" relationship. Take a look at this page: http://datamapper.wanwizard.eu/pages/advancedrelations.html

Go down to the section titled "Many-to-Many Reciprocal Self Relationships" and see if that helps you.

In essence, it should be a matter of adding reciprocal to the properties:

<?php
class Prospect extends DataMapper{

    var $has_one =array(
        'parent' => array(
            'class' => 'prospect',
            'other_field'=>'prospect',
            'reciprocal' => TRUE
        ),
        'prospect' => array(
            'other_field' => 'parent', 
            'reciprocal' => TRUE
        )
    );
}
swatkins
  • 13,530
  • 4
  • 46
  • 78
  • parent is prohibited, based on my source code of datamapper -> parent is used and It shouldn't be use to create relation – Pawel Jan 08 '13 at 14:40
  • Reciprocal Self Relationships are needed when you want to maintain the relation both ways, and you have a setup with two FK's (a many-2-many or has_many's both ways). If enabled and you create a relation, the relation in the other direction is added automatically. – WanWizard Jan 21 '13 at 17:37
  • WanWizard -> yes, I know. It would be good if you publish list of the restricted namespaces (as parent). BTW: you library is great :) THX for it – Pawel Jan 31 '13 at 05:26
0

OK, Problem solved
variable parent is used in Datamapper library class to keep an array with relations, so 'parent' is restricted and it couldn't be used at all. i.e. in my case. I have changed from parent to parent_company and all is working brilliant. (this is solved my left join problem as well).

Pawel
  • 21
  • 3