Nanne Huiges Thu Jan 19 08:45:30 -0500 2012

Subject: Date object vs string depending on capitalizaion?

I have seen below behaviour, and I don't really know where it comes from, if this is a deliberate case (I hope not) and or if someone knows a patch:
A date/datetime field in the database gets converted to a DateTime object, but NOT if the columname has a capital.
This means that if I do this:

1     CREATE TABLE `test` (
2           `id` INT NOT NULL, 
3           `testDate1` DATE NOT NULL, 
4           `testdate2` DATE NOT NULL)
5     ENGINE = InnoDB;

And make an object out of it, I get 1 string for testDate1, and one ActiveRecord\DateTime Object. Same will not happen if I make the first column all lowercase.

This is kinda tricky if I want to use a AR-model, because I need to decide if I have to do '->format()' or can't.


Nanne Huiges Fri Jan 20 06:09:45 -0500 2012

The issue seems to come from the function

1 assign_attribute($name,$value)

This function decides if 'cast' should be called on the value, on the basis of the $name being present as a key in the "$table->columns" array.

The name is the 'inflected' name, but the array_keys are the origional table-column names.

Possible sollutions might be:
  • make the keys of the columns->table the inflected name (don't know what effect that might have, we might need to call 'lower' on some fields)
  • fix the function to check the inflected name if the key is not found like so.
1 if (array_key_exists($name,$table->columns) && !is_object($value)){
2     $value = $table->columns[$name]->cast($value,static::connection());
3 }else{//new check: maybe the $name is inflected.
4     $col = $table->get_column_by_inflected_name($name);
5     if (!is_null($col) && !is_object($value)){
6         $value = $col->cast($value,static::connection());
7     }
8 }
Nanne Huiges Wed Feb 15 11:36:17 -0500 2012

No-one any other thoughts?

Nanne Huiges Wed Mar 14 10:49:15 -0400 2012

I've made some changes and a test, i'll continue this on git

(1-3/3)