I’m working with DataMapper ORM in Codeigniter 2.1.2:
I have this kind of entities:
Person
-id PRIMARY KEY
-name
-lastname
-age
Student
-person_id PRIMARY KEY
-college
-notebook_color
-other attribute
Address
-id PRIMARY KEY
-street
-number
-person_id
NOTE: Student has no own PK. It uses Person’s ID as PK.
So my problem is. I want to create a student:
$student = new Student();
$student->get_by_person_id($id);
I want $student to have all the data from person, including the addesses. I tried using include_related() to bring the fields from person, but this doesn’t bring the objects related.
I would like my $student look like:
$student ->id
->name
->lastname
->age
->college
->notebook_color
->other_attribute
->address (array)
[0] -> id
-> street
-> number
[1] -> id
-> street
-> number
How can I get this to work?
You can not have a PK called ‘person_id’. Datamapper requires all PK’s to be called ‘id’. And FK’s should be called by their relation name, suffixed with ‘_id’. So the FK in address should be called ‘student_id’.
Also, Datamapper doesn’t support composites. So there is no way to merge records from Person and Student.