Welcome Guest, Not a member yet? Register   Sign In
datamapper two relations to same model
#1

[eluser]TomMRiddle[/eluser]
we have a to and from user relations on a recommendation table. we dont know how to write this in datamapper(DMZ). How do we write the relationship in the models for both the User model and the recommendation model?

We have made what we think is right in the recommendation model, but don't know for sure, and we are completely lost about how to write it all in the user model.

in recommendation model:
Code:
$has_one = array(
        'from_user'=> array(
            'class' => 'user',
            'other_field' => 'from_user_id'
        ),
        'to_user'=> array(
            'class' => 'user',
            'other_field' => 'to_user_id'
        )
);

An example on how to access the relation from either end would be much appreciated too

thankful for your help
#2

[eluser]WanWizard[/eluser]
Check the manual:

http://datamapper.wanwizard.eu/pages/adv...tions.html, "Multiple Relationships to the Same Model".
#3

[eluser]TomMRiddle[/eluser]
We spent hours on this, can't believe we missed it when it was right in there...
So I guess this is how we should do it:
Code:
class User extends DataMapper {
    $has_many = array(
        'recommendation_from' => array(
            'class' => 'recommendation',
            'other_field' => 'from_user'
        ),
        'recommendation_to' => array(
            'class' => 'recommendation',
            'other_field' => 'to_user'
        )
    );
}

class Recommendation extends DataMapper {
    $has_one = array(
        'from_user' => array(
            'class' => 'user',
            'other_field' => 'recommendation_from'
        ),
        'to_user' => array(
            'class' => 'user',
            'other_field' => 'recommendation_to'
        )
    );
}

and then We access them like this:
Code:
$r = new Recommendation($id);
$r->to_user->get();
$r->from_user->get();

or

$u = new User($id);
$u->recommendation_from->get();
$u->recommendation_to->get();

The corresponding database tables:
users:
id - INT primary

recommendations:
from_user_id - INT FK users id
to_user_id - INT FK users id

Correct? ... I guess we will find out ourself when we try it tomorrow.

tnx for the help
#4

[eluser]WanWizard[/eluser]
Correct.

Note that the ITFK's in Recommendation would be 'from_user_id' and 'to_user_id' (i.e. the custom model name, followed by '_id').




Theme © iAndrew 2016 - Forum software by © MyBB