[eluser]godunov[/eluser]
I'm using DataMapper DMZ, trying to make a flashcard here and I have a
Card class and a
Card_Side class. Each instance of Card_Side has a relationship with exactly one Card, and each instance of Card has exactly one relationship with
two different instances of Card_Side, one called
front and the other
back. SInce these are one-to-one in both directions, I am not using a join table (for the record using a join table doesn't seem to change anything).
My tables (table_name => attributes):
cards => id, front_id, back_id
card_sides => id, card_id
My Relationships:
Card:
Code:
var $has_one = array(
'front' => array(
'class' => 'card_side'
),
'back' => array(
'class' => 'card_side'
)
);
Card_Side: *I've tried many things here, nothing works for me
Code:
var $has_one = array(
'card' => array(
'other_field' => 'back'
),
'card' => array(
'other_field' => 'front'
)
);
// Also Tried:
var $has_one = array('card');
Main Script to Create Card and Card_Sides
Here I create a Card, two Card_Sides, and relate them to each other:
Code:
$this->load->model('card', 'card_model');
$this->load->model('card_side', 'side_model');
$empty_card = $this->card_model->create();
if ($empty_card != NULL)
{
$front = $this->side_model->create();
$front->save($empty_card);
$back = $this->side_model->create();
$back->save($empty_card);
echo '$front->id = '.$front->id.'<br>';
echo '$back->id = '.$back->id.'<br>';
$empty_card->save(
array(
'back' => $back,
'front' => $front
));
echo '$empty_card->front->get()->id = '.$empty_card->front->get()->id.'<br>';
echo '$empty_card->back->get()->id = '.$empty_card->back->get()->id.'<br>';
echo '$front->card->get()->id = '.$front->card->get()->id.'<br>';
echo '$back->card->get()->id = '.$back->card->get()->id;
}
Tables After This Code Runs:
cards:
+------+----------+---------+
| id | front_id | back_id |
+------+----------+---------+
| 8239 | 85 | 86 |
+------+----------+---------+
card_sides
+----+---------+
| id | card_id |
+----+---------+
| 85 | NULL |
| 86 | 8239 |
+----+---------+
Here's my problem. Athough the card's "back_id" and "front_id" fields are filled with the correct ids of the two new Card_Sides, the above code doesn't relate the objects together properly. Also the first Card_Side is getting a NULL for its card_id. The echo statements in the code above print the following:
$front->id = 85
$back->id = 86
$empty_card->front->get()->id = 86 :down:
$empty_card->back->get()->id = 86
$front->card->get()->id = 8239
$back->card->get()->id = :down:
So the Card is only associated with the second Card_Side ("back"), and only the first Card_Side ("front") is associated with the Card.
How do I set up my relationships for Card_Sides here? All the examples I've seen of multiple references to the same class have has_many in at least one direction. How do we do has_one in both directions?
Many thanks for any input.