Question

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.

Was it helpful?

Solution 2

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).

OTHER TIPS

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
        )
    );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top