CodeIgniter Forums
DataMapper ORM v1.8.2 - 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.2 (/showthread.php?tid=47243)



DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]WanWizard[/eluser]
It echo's
Code:
0: Date one
1: Date four
1: Date three
2: Date two



DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]Maglok[/eluser]
Which is all 4 from the 4 objects. So it seems to echo them all. Hence it must have them all while I use a simple '=' operator to assign the object to the $ch->dates; Which is why I am confused that should simply not work like this.


DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]WanWizard[/eluser]
Well,

Your original code didn't fetch anything, so it didn't echo anything. I have to explicitly fetch a record in the index method using
Code:
$ch->where('id', $id)->get();

I get this output because in my test config both 'auto_populate_has_many' and 'auto_populate_has_one' are set to true.

Which means that as soon as you access $this->events, Datamapper will fire a query to populate it. If you don't want that, you can disable this behaviour in the config, in the model, or in the relationship definition (depending on the desired scope).


DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]Maglok[/eluser]
Aha! I see my misinterpretation. As I suspected it is small. I basically thought I did this:

Code:
public function calculate()
{
  // set dates
  $this->dates = new CCDate();
  
  foreach($this->events as $event)
  {
   $this->dates = $event->dates;
  }

  $i = 0;
  foreach($this->dates as $date)
  {
   echo $i . ': ' . $date->name . '<br>';
  }
  $i++;
}

But of course I put the foreach in the first foreach, thus making it echo for each assignment before it changes to the next assignment.

Remember kids: Sometimes you just need a slap against the head.

So now I can work on actually adding them together instead of just assigning one, echo-ing values, assigning the other. Thanks! Smile

I blame the heat, here in holland it got up to 36 degrees celsius.


DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]Ramania[/eluser]
@wanwizard, thanks for the amazing ORM, i have a couple of fast questions if you could kindly answer them :

1. i am using get_paged_iterated, is there anyway i can get the first and/or last items of the protected 'result' object? something like $results->first without doing a foreach loop?.

2. Is there any plan or a quick work around for polymorphic associations?.

Thank you and waiting your response.


DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]WanWizard[/eluser]
[quote author="Maglok" date="1345543896"]I blame the heat, here in holland it got up to 36 degrees celsius.[/quote]
Ik weet er alles van. Smile


DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]WanWizard[/eluser]
[quote author="Ramania" date="1345544764"]1. i am using get_paged_iterated, is there anyway i can get the first and/or last items of the protected 'result' object? something like $results->first without doing a foreach loop?.[/quote]
You can access the loaded objects using $object->all. This is a normal array, so you can use reset(), end() etc to get elements from it.

[quote author="Ramania" date="1345544764"]2. Is there any plan or a quick work around for polymorphic associations?.[/quote]
Short answer: yes. no.


DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]Ramania[/eluser]
[quote author="WanWizard" date="1345549198"][quote author="Ramania" date="1345544764"]1. i am using get_paged_iterated, is there anyway i can get the first and/or last items of the protected 'result' object? something like $results->first without doing a foreach loop?.[/quote]
You can access the loaded objects using $object->all. This is a normal array, so you can use reset(), end() etc to get elements from it.

[quote author="Ramania" date="1345544764"]2. Is there any plan or a quick work around for polymorphic associations?.[/quote]
Short answer: yes. no.[/quote]

Thank you, since get_paged_iterated has an empty 'all' object, i used get_paged instead, according to the guide this has some performance issue, but i don't think it matters when i am getting 20 items at a given time, i hope i am right.


DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]WanWizard[/eluser]
Ah, ok, missed that.

No, with 'iterated' you can't do that, as it will fetch an object at the time from the PHP driver (like with mysql_fetch_row()), so Datamapper doesn't know what 'first' and 'last' is.

As you correctly observed, the iterated variant is really useful when you have large datasets to you have to process sequentially, but if your datasets are small or you need random access, don't use it.


DataMapper ORM v1.8.2 - El Forum - 08-21-2012

[eluser]Maglok[/eluser]
Awesome Wanwizard it totally works now. Smile I have done several even and they all work quite nicely. Kinda like this:

Code:
// set dates
  $this->dates = new CCDate();
  
  foreach($this->events as $event)
  {
   $this->dates->all = array_merge($level->dates->all, $this->dates->all);
  }

Amen!

Now can I get a 'yes please' for a 'resort()' method? Hallelujah!

More sun! *hiss*