Advanced Relationship
Hi all. I’m not able to understand how advanced relationships work.
I’ve read user guide and made tests, but I can’t understand how field names are used.
This is my sample case:
- table users(id, name)
- table quotes(id, client_id, worker_id)
- user model and quote model
One user (worker_id) can create a new quote for other user (client_id).
‘worker_id’ and ‘client_id’ are in fact ids from users table.
How should I make relationships? This is what I’ve done:
class Quote extends DataMapper {
var $has_many = array(
'client_id' => array('class' => 'user','other_field' => 'client'),
'worker_id' => array('class' => 'user','other_field' => 'worker')
);
}class User extends DataMapper {
var $has_one = array(
'client' => array('class' => 'quote', 'other_field' => 'client_id'),
'worker' => array('class' => 'quote', 'other_field' => 'worker_id')
);
}Is that right? And from my controller, how should I populate this? I want to get something like this:
<?php
$q = new Quote($id);
$q->client->get();
echo $q->client->name;
$q->worker->get();
echo $q->worker->name;
?>Please, help me understanding this relationships.
Thanks all