Welcome Guest, Not a member yet? Register   Sign In
Datamapper and populating related objects in array of objects
#1

[eluser]night_day[/eluser]
Hi everyone, just starting out with CodeIgniter and Datamapper, wonderful stuff so far.

I've run into an issue with populating related objects. The objects save properly and all the relationships save ok.

Here are the objects

User
Item (has many notes)
Note (has one item, has one user)

$item = new Item();
$item->where("id", id);
$item->get();

// this works ok and returns my one item

$notes = $item->note->get();

// this works ok as well, $notes->count() returns 2

$notes->user->get();

// this is where I have problems, I assumed that this would get the related user object for each note in the notes array of objects but doesn't seem to work even though there is no errors. Can anyone be of assistance? Thanks!
#2

[eluser]BrianDHall[/eluser]
Sure, when you call $notes->user->get() you get all the associated users related to the first note, but nothing automatically iterates through notes and calls ->user->get() again, I'm afraid. I believe I asked a very similar question as this to OverZealous, and I just can't remember how he suggested to handle it.

I remember that one way was to hack it a bit, in that I foreached() through $notes->all, called ->user->get(), then assigned $notes->user to a $variable[] so when it was done I had an array of users related to all $notes, but the problem there is it assumed 1 user per note. If there can be multiple users per note then that doesn't work at all.

I think OZ recommended something like one of the magic Advanced Get methods, possibly with adding a distinct() there, but I hadn't had a chance to test it out to fully wrap my head around it. Advanced Get and the use of distinct() with magic functions is sort of 'the next thing on my list' to learn in ORM Smile




Theme © iAndrew 2016 - Forum software by © MyBB