¿Cómo obtener () relaciones de 3 niveles de profundidad en el mapeador de datos en el encendedor de código?

I am designing a database with the code igniter datamapper. Imagine that I have a three tables one with users and one with friends and one with foods friend like. I am trying to access the food friend like. I have populated all the tables with data mapper. Here is how I do it:

user mesa tiene id, user_name, gender, user_id campos.

Every user has many friends and friend table is:

friend mesa tiene id, friend_id, friend_name, user_id

Every friend has many foods and here is table for it: friend id food

I was able to populate the tables by filing out the values. Lets say for example:

user:
1 andy male 394 
2 jen  female 439
..

friend:
1 243 mark 394
2 493 silvia 394
3 459 gloria 439
...

So when I try to access friend of a user from the database I do this:

$u->get_by_user_id($data['user']['id']);

if ($u->exists())
{ 
$data['user']['first_name'] = $u->first_name;
$data['user']['last_name'] = $u->last_name;
$data['user']['gender'] = $u->gender;
$data['user_friends']=$u->friend->get();

and it works...(i don't know why it knows what is the id of friend to relate to) but it works... now I am trying to go one level deeper and access or get() every users' friends foods...but I do't know how?

preguntado el 01 de febrero de 12 a las 22:02

1 Respuestas

Ok I found the answer:

 if ($u->exists())
{ 
$data['user_profile']['first_name'] = $u->first_name;
    $data['user_profile']['last_name'] = $u->last_name;
$data['user_friends']=$u->friend->get();

foreach($u->friend->get()->all as $u){

$fm=new Food();
$fm->get_by_friend_id($u->friend_id)->get();

foreach($fm->all as $fm){
//stuff
          }
}

Respondido 03 Feb 12, 06:02

It'd be great if you could answer your own question with specifics on the configuration required to do this. That way, you can accept your own answer and this thread remains a good resource for people with a related issue in the future.. - landons

Si tomas el $fm = new Food(); out of the foreach-loop, the performance will increase! Just use $fm->clear(); at the top inside the loop. Connections between models are done through relationsships: Check the datamapper docs. - Ruben Müller

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.