CodeIgniter Forums
DataMapper ORM v1.8.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper ORM v1.8.0 (/showthread.php?tid=37531)



DataMapper ORM v1.8.0 - El Forum - 06-01-2011

[eluser]rherriman[/eluser]
Result sets returned by get() or get_iterated() are not arrays. They are iterable objects. As WanWizard said, this is why you must use get_clone()--to "copy" the object. This is what I'd recommend, as well.

If you really want to have the results in an associative array however, this is possible with the "array" extension: http://datamapper.wanwizard.eu/pages/extensions/array.html


DataMapper ORM v1.8.0 - El Forum - 06-01-2011

[eluser]Arministrator[/eluser]
[quote author="rherriman" date="1306979083"]Result sets returned by get() or get_iterated() are not arrays. They are iterable objects. As WanWizard said, this is why you must use get_clone()--to "copy" the object. This is what I'd recommend, as well.

If you really want to have the results in an associative array however, this is possible with the "array" extension: http://datamapper.wanwizard.eu/pages/extensions/array.html[/quote]

Right, I understand this, but the second portion of your reply is the answer I was really looking for. I will definitely try the cloning method too, it does seem to better suit my needs in this particular example.

Thanks, wanwizard, rherriman, really helpful.


DataMapper ORM v1.8.0 - El Forum - 06-02-2011

[eluser]Arministrator[/eluser]
What the hell am I doing wrong?

Code:
$donor = new Donor;
$donor->where(...)->include_related(...)->get();
$donor->donation->get_iterated();

This works out as expected, foreach loop gets all donations.

BUT, adding:

Code:
$donor_c = $donor->get_clone();
$donor_c->donation->order_by(...)->limit(...)->get;

This breaks the code. The foreach loop for the original object only gets the number of results limited by the clone's limit() query, which tells me data is overwritten.

Shouldn't it be like in the docs: "both the $clone and $u objects are identical except you can modify the data of one, without affecting the data stored in the other." And there's also stuff about clone's related objects becoming clones, rather than instances, which all sounds great.

What did I get wrong?

EDIT: from the start i knew if I instantiate a new Donor object, I'll be fine.

If I instantiate a new Donor, with the first donor's id, I basically get two identical instances of those objects, run separate queries on them, pass and data and do anything else - problem solved. But I need to understand stuff, so this is not enough for meSmile

Why didn't my cloning work. Maybe because get_iterated doesn't create objects, so proper cloning of related objects couldn't be done? How to get around this? Do get() instead of get_iterated()?

If I'm too annoying, just tell me to bug off.


DataMapper ORM v1.8.0 - El Forum - 06-02-2011

[eluser]WanWizard[/eluser]
Ah, yes. When you clone an object, you're only cloning that object.

Variables or object properties that contain other objects, are actually aliases of pointers to the same object. When you clone the object, the aliases are cloned as well, but will still point to the same object instance. Which means that $donor->donation will point to the same object as $donor_c->donation.


DataMapper ORM v1.8.0 - El Forum - 06-02-2011

[eluser]Arministrator[/eluser]
So, a new object. My guts told me the truth this time.

However, my guts didn't help me with this next puzzle:

I'm trying to get a paged list of donors. get_paged_iterated does the trick. Foreach loop then displays the results just fine. And just when I started to think DataMapper's getting gentle on me, trying to read related table's created field gives me nothing.

The code:
Code:
// controller
$donor = new Donator;
$donor->donation->order_by('created', 'desc')->limit(1)->get();
$donor->get_paged_iterated($page, 20);

// view

<?php foreach ($donor as $d): ?>
<li>
   &lt;?php echo $d->name . ' ' . $d->email; ?&gt;
   &lt;?php echo $d->donation->created; ?&gt; // <--- The problem child
</li>
&lt;?php endforeach; ?&gt;

I remember just about the first time i tried to use DataMapper this happened, and I handled it, but I cannot for the life of me remember how I did it.

A quick note: I do try Google and forums search every time before I post.


DataMapper ORM v1.8.0 - El Forum - 06-02-2011

[eluser]bEz[/eluser]
nvm


DataMapper ORM v1.8.0 - El Forum - 06-02-2011

[eluser]Arministrator[/eluser]
[quote author="bEz" date="1307045704"]nvm[/quote]

I figured if I don't pass an id and create an object it will return all rows. And dump shows it does. So if I do a limit 1 query for a related object, I hoped it will get a single row for every object. I guess I hoped wrong.

Any suggestions on how to do it right? It just popped in my head that I could get a related object within a foreach loop, but I guess views should be used just for displaying stuff, not interacting with the database, so even if it works, there must be a 'proper' way.


DataMapper ORM v1.8.0 - El Forum - 06-02-2011

[eluser]WanWizard[/eluser]
Haven't got a clue what you mean by that last post.

But your code is a bit odd. First, you fetch the donor that has done the most recent donation. Then you run a new query on $donor, destroying your first result.
Then you loop over the results of that second query, and try to access a related object (donation) which was never fetched?


DataMapper ORM v1.8.0 - El Forum - 06-02-2011

[eluser]bEz[/eluser]
Well, that's why I "nvm'd" my reply, because I'm at work and could not really tell what @Arministrator was trying to debug.


DataMapper ORM v1.8.0 - El Forum - 06-02-2011

[eluser]Arministrator[/eluser]
[quote author="WanWizard" date="1307060545"]Haven't got a clue what you mean by that last post.

But your code is a bit odd. First, you fetch the donor that has done the most recent donation. Then you run a new query on $donor, destroying your first result.
Then you loop over the results of that second query, and try to access a related object (donation) which was never fetched?[/quote]

Right...

I need to get the list of all donors and each donor's last donation. Could you please point to a solution?