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

[eluser]WanWizard[/eluser]
please define "resort() method"? What is that supposed to do?

[eluser]Maglok[/eluser]
If I array_merge it all together I add together 4 or so sets all with their own internal ordering.

Example:
Object 1: Bob, Lucy, Zacharias
Object 2: Dirk, Theo

Add em together: Dirk, Theo, Bob, Lucy, Zacharias
Wanted: Bob, Dirk, Lucy, Theo, Zacharias

I bet I could do something with $object->all and then a sort array or so.

[eluser]WanWizard[/eluser]
Guess that should be an extension method, reordering the all array (basically doing an ORDER BY after the facts) on an arbitrary value is not something you need on a daily basis.

[eluser]Ayuniverse[/eluser]
Hi, guys,

I'm having an issue with query(). I'm populating my object this way cause it is a complicated request. The thing is, I want my object to be populated also with the fields I add in the select statement that are not from the object table. How do I do that ?

Thank you !

[eluser]WanWizard[/eluser]
Can you be more specific? Anything that is part of the select will be part of the result.

[eluser]Ayuniverse[/eluser]
Event if I do this ?

$user->query(select user.username, user.avatar, othertable.othercolumn from ...)

In that case, how do I access it ? $user->othercolumn ?

Thank you for your help, btw

[eluser]Maglok[/eluser]
@Wanwizard: Well the way I see it I have a datamodel that has objects and those objects come with more objects. I need access to a full list of those objects quite often so for me this comes up a bunch. Imma gonna dig in on it.

@Ayuniverse: I haven't had to use query() yet. It sort of seems you want fields that are not part of the table that the object is based on to show up as values in the object. The general idea is that if it is another (not join) table that it is then its own object that is referenced in the main object.

Ah found a quote in the manual: "The Query method will populate the object with the results so it is very important to remember that you should be querying for data from the objects table." So I feel like it should not work, though it is just an educated guess.

[eluser]Maglok[/eluser]
Code:
/**
  * Sort Again
  * Takes a array of objects and sorts the default_order_by of the object.
  *
  * @param array $array Array of objects to sort.
  * @return array The array sorted by the default_order_by.
  */
function sort_again($array)
{
  // Do the sort
  usort($array, create_function(
        '$obj1, $obj2',
        '$default = $obj1->default_order_by[0]; return strnatcasecmp($obj1->$default, $obj2->$default);') );
  
  return $array;
}

This re-sorts a datamapper array by default_order_by. It isn't pretty, but sorting objects isn't my strong suit. If anyone is interested, it can be extended to default to default_order_by, but also have a parameter 'property' to sort by a different property. Couldn't get that to work in the 5 min I had, because the sort function of usort will not allow more then two parameters.

[eluser]WanWizard[/eluser]
[quote author="Ayuniverse" date="1345617551"]Event if I do this ?

$user->query(select user.username, user.avatar, othertable.othercolumn from ...)

In that case, how do I access it ? $user->othercolumn ?
[/quote]
Every column in the resultset will be mapped one-on-one with an object property. So in this case $user->othercolumn, yes.

[eluser]Ayuniverse[/eluser]
OMG... Yeah it was working from the beginning... I made a mistake in the result manipulation ><

Sorry about that xD

Thank you guys for your help !




Theme © iAndrew 2016 - Forum software by © MyBB