Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]Maglok[/eluser]
Anyone know of a good way to combine result sets of the same object through datamapper?

Subgroup of Person + subgroup of Person = group of People?

[eluser]WanWizard[/eluser]
Quick and dirty:

Code:
$subgroup1->all += $subgroup2->all;

Without having tested it, but this should work if you use "all_array_uses_ids" setting. If not, then you're all array indexes are not unique, and you'll have to use array_merge().

[eluser]Maglok[/eluser]
Yeah I tried that first, but I got cast errors. I tried array_merge next, but that kept making it end up in an array (after messing around with casting to objects for a while).

Then I just tried this:

Code:
// set stuff
  $this->stuffz = new Stuff();
  
  $i = 0;
  foreach($this->stuffcontainers as $stuffcontainer)
  {
   $this->stuffz = $stuffcontainer->stuffz;

                        // check what is added together
   foreach($this->stuffz as $stuff)
   {
    echo $i . ': ' . $stuff->name . '<br>';
   }
   $i++;
  }
Based on data:
- $this is a datamapper object
- $this->stuffcontainers = a has_many of the object Stuffcontainers
- A stuffcontainer object has_many Stuff objects.
- $this->stuffz = empty till I call the above method

I was quite surprised to see this work. If $this->stuffcontainers contains 3 instances of the Stuffcontainer object then I was kind of expecting to just see the stuff value from the last assigned to $this->stuffz. Instead they are all assigned. I am confused as to why.

[eluser]WanWizard[/eluser]
That's odd. I just tried this in a controller:
Code:
// merge two object results into a third object
public function merge()
{
  $u1 = new User();
  $u1->where('status_id', 1)->get();

  $u2 = new User();
  $u2->where('status_id', 2)->get();

  $u3 = new User();
  $u3->all = array_merge($u1->all, $u2->all);

  foreach($u3 as $u)
  {
   echo $u->id, $u->name, $u->status_id, '<br />';
  }
}
and this works fine. Had to use array_merge() here, this app doesn't map id's to all indexes.

[eluser]WanWizard[/eluser]
[quote author="Maglok" date="1345205439"]
I was quite surprised to see this work. If $this->stuffcontainers contains 3 instances of the Stuffcontainer object then I was kind of expecting to just see the stuff value from the last assigned to $this->stuffz. Instead they are all assigned. I am confused as to why.[/quote]
Quite logical, they are all different instances of Stuffz...

[eluser]Maglok[/eluser]
I don't quite follow.

I assign a new object to $this->stuffz every iteration. The objects I assign do not know the other objects, so how could it all stack up? I am not using +=, but =.

[eluser]WanWizard[/eluser]
You start with
Code:
$this->stuffz = new Stuff();

but in your foreach loop you have
Code:
$this->stuffz = $stuffcontainer->stuffz;
which will overwrite the newly created object.

So if stuffcontainer contains 3 objects, and each of these 3 objects have 3 related stuffz, you'll loop 9 times.

[eluser]Maglok[/eluser]
Well then yes, but each of the objects I foreach over do not.

Stuffcontainer 1 has 1 stuff
Stuffcontainer 2 has 1 stuff
Stuffcontainer 3 has 2 stuff

[eluser]WanWizard[/eluser]
Hmmm... Difficult to discuss without being able to actually see and test the code.

[eluser]Maglok[/eluser]
Aight Smile I am sure it is some silly thing but here ya go:

(stripped for not-relevant stuff)
Code:
http://www.maglok.nl/test.rar

At this point I am mainly curious why it is 'working' while in my head it should not. I could use it and then write a sort function for the objects and voila it is workable, I rather know why though.

If anyone has any idea and some time to spare lemme know.




Theme © iAndrew 2016 - Forum software by © MyBB