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

[eluser]Bard[/eluser]
Hi,
How can i order by field in datamapper?
ORDER BY FIELD(id, 5, 1, 3, 2, 4)

thanks

[eluser]diZzyCoDeR[/eluser]
Phhfft, ignore my contribution, WanWizard was paying attention and new what Bard wanted.

:red:

-----
@Bard: http://datamapper.wanwizard.eu/pages/get...r.Features

which I found by searching:

Quote:"order by" site:datamapper.wanwizard.eu

in google.

[eluser]WanWizard[/eluser]
Datamapper uses CI's active record to define order_by(), by calling $this->db->order_by().

A quick peek in the AR class tells me that it is not going to work. If the first argument contains a comma's it will be broken up and escaped, if the second argument contains anything other then 'ASC' or 'DESC' it will default to 'ASC'.

So go onto github and create an issue for this to it can be fixed in CI.

[eluser]Bard[/eluser]
here is a quick solution for this issue,
in system/database/DB_active_rec.php
Code:
public function order_by($orderby, $direction = '')
just after this, add;
Code:
if (strpos($orderby, 'FIELD(') !== FALSE || strpos($orderby, 'FIELD (') !== FALSE){
$this->ar_orderby[] = $orderby;
return $this;
}
and you can use like that;
Code:
$object->order_by('FIELD(id, 5, 1, 3, 2, 4)');

[eluser]WanWizard[/eluser]
That'll work, but to get this supported you need to get it into the official code.

And I'm afraid this is going to be seen as a bit of a hack. Did you create a ticket at http://github.com/ellislab/codeigniter/issues so the developers are aware of this issue?

[eluser]pyweb1[/eluser]
hi, thx for the last answer, it was very usefull!

i am trying to get my created and updated feild working, they are supposed to be automatic. I created 2 column in my database called 'created' and 'updated' with the type datetime, im using phpmyadmin btw.

now I tri to see some news and it work very well:
Code:
$n = new News();
$n->limit(10)->get();
foreach($n as $news){
echo $news->title . '<br />';
}
and i get 10 post title!

if I replace this code with:

Code:
$n = new News();
$n->get_by_id(3);
$n->title = 'asdasdasd';
$n->save();

I get my new title updated ('asdasdasd') on the db but the updated field is still full zeros!

I use this config in confin/datamapper.php
Code:
...
$config['created_field'] = 'created';
$config['updated_field'] = 'updated';
$config['local_time'] = FALSE;
$config['unix_timestamp'] = FALSE;
$config['timestamp_format'] = '';
$config['lang_file_format'] = 'model_${model}';
...

what do I do wrong?

[eluser]WanWizard[/eluser]
How did you define the created and updated fields in the database?

You set unix_timestamp to FALSE, so I assume MySQL DATETIME? In that case you can't leave the timestamp_format empty. Either don't define it at all (in which case Datamapper will use the default), or define it yourself.

[eluser]sevenpointsix[/eluser]
[quote author="Stolz" date="1338301630"]Thanks WanWizard, commenting out the $config['timestamp_format'] for using the DM default did the trick Smile[/quote]

Just to echo what Stolz said about this. If you leave the 'timestamp_format' variable in the config file as an empty string (which is the case with the config file that you get when you download DataMapper), then the automatic created/updated timestamps won't work. You have to comment the line out -- or, manually set this to the correct timestamp format, which is probably "Y-m-d H:iConfused O" for most users. (If you want to use BST, make sure that 'local_time' is set to TRUE as well).

This caught me out, because with previous versions of DataMapper, it was fine to leave this config value empty, and the default timestamp above was used. However, in the latest version, this is not the case. I don't think this is a bug as such -- more that the standard config file probably shouldn't set 'timestamp_format' to an empty string by default.

(Sorry -- only just noticed that WanWizard gives exactly this answer in the post above!).

Many many thanks as always to WanWizard for all his work. DataMapper is an absolutely fantastic tool -- I wouldn't build a site without it.

[eluser]WanWizard[/eluser]
Config values are merged with model class properties to get the final config value for a specific model.

Older versions used empty() to test for a value, which went horribly wrong when you have FALSE, 0 or "" as a valid config value, so the way config values are processed has changed in 1.8.2.1. Which this side effect as a result.

In the latest version on Bitbucket the default has already been changed, but obviously the distribution version doesn't have this yet.

[eluser]coldscooter[/eluser]
Is there a version of this DataMapper for native PHP which does not use the CI framework? I am currently working on migrating some legacy code into a CI framework and I would like to implement this ORM into the legacy code to help ease the transition.

Thanks in advance.




Theme © iAndrew 2016 - Forum software by © MyBB