Welcome Guest, Not a member yet? Register   Sign In
[Solved] DataMapper reciprocal one to many self relationship
#1

[eluser]Dracos[/eluser]
I'm having trouble setting up a reciprocal self relationship in DM, which is one-to-many (essentially a parent-child scenario).

The events table:

Code:
id
basis_event_id

Data:

Code:
+-----+----------------+
| id  | basis_event_id |
+-----+----------------+
| 414 |              0 |
| 415 |            414 |
| 416 |            414 |
| 417 |            414 |
| 418 |            414 |
| 419 |            414 |
+-----+----------------+

In the model:

Code:
$has_one = array(
    'basis_event' => array(
        'class' => 'event',
        'other_field' => 'event',
        'reciprocal' => true
    )
);
$has_many = array(
    'recurrences' => array(
        'class' => 'event',
        'other_field' => 'basis_event',
        'reciprocal' => true
    )
);

So when I do this:

Code:
$event = new event(414);
$event->recurrences->get();

I get events 415 through 419, which is expected, via this query:

Code:
SELECT `events`.*
FROM (`events`)
WHERE `events`.`basis_event_id` = 414

But I can't figure out the other side of the relationship.

Code:
$event = new event(415); // one of the recurrences
$event->basis_event->get();

Throws an error, or breaks recurrences, or hits my memory limit. I need to generate this query:

Code:
"SELECT `events`.*
FROM (`events`)
WHERE `events`.`id` = " . $this->basis_event_id

I suspect I'm masking basis_event_id with the working relationship, but anything I do to that breaks it. I'm also sure I'm missing something, although setting reciprocal=true doesn't seem to make a difference.


Messages In This Thread
[Solved] DataMapper reciprocal one to many self relationship - by El Forum - 12-15-2011, 03:19 PM



Theme © iAndrew 2016 - Forum software by © MyBB