0

I use datamapper orm with codeigniter

my table is:

person id, name, related_person1_id, related_person2_id

person can have 1 related_person1, 1 related_person2

how can i set the relationship in my model file ?

it is described in the docu:

class Person extends DataMapper {
$has_many = array(
    'related_person' => array(
        'class' => 'person',
        'other_field' => 'person',
        'reciprocal' => TRUE
    ),
    'person' => array(
        'other_field' => 'related_person',
        'reciprocal' => TRUE
    )
);

}

how can I set in for more than 1 related person ? and how shold i set up my table ? i wanna stay with 1 "person" table.

THX

John Smith
  • 47
  • 8

1 Answers1

0

Best bet is to create a separate table for relationships.

So you have a person table:

id:int
name:text

then a relationship table

id:int
person_id:int
related_to_person_id:int
relationship

This way you can have as many relationships per person as you want.

Persons:

id | name
 1 | Jimmy Doublechin
 2 | Opie Hardabs
 3 | Anthony Predator

Relationships:

id | person_id | related_to_person_id | relationship
 1 |         1 |                    2 | lover
 2 |         1 |                    3 | best buddy evar
 3 |         3 |                    2 | Mother

Then

class Person extends Datamapper {
    public $has_many = array('relationships');
...


class Relationships extends Datamapper {
    public $has_one = array('person');
....
stormdrain
  • 7,915
  • 4
  • 37
  • 76