Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

DateTime object casting for capitalized fields #188

Closed
wants to merge 3 commits into from

2 participants

@NanneHuiges

If you have a capitalized field, it doesn't get called trough "cast", so it stays a string.

A simple edit to check that when the array-key doesn't exist, maybe the inflected name does.

I've even written a test for it, but bear with me because I'm not very well known with phpunit or the defaults of how to work with a database in it. I've changed the definition of the authors table to be able to test something:
With this you see that without the changed model you'll get an error (some_Date is a string, and not an object), and with the changed model the test runs fine.

CREATE TABLE authors(
    author_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    parent_author_id INT,
    name VARCHAR(25) NOT NULL DEFAULT 'default_name',
    updated_at datetime,
    created_at datetime,
    some_Date date,
    some_time time,
    some_text text,
    some_enum enum('a','b','c'),
    encrypted_password varchar(50),
    mixedCaseField varchar(50)
) ENGINE=InnoDB;
NanneHuiges added some commits
@NanneHuiges NanneHuiges Changed assign_attribute so that fields with a different inflected na…
…me (e.g. capital letters) get casted too

For instance: if you have a field called 'ThisIsADate' it would not become a datetime object.
If the old array_key_exists check fails, a second check is done using the inflected name.
83eec44
@NanneHuiges NanneHuiges Testing the 'datefield-with-capital' bug
Needs a datefield with a capital letter. I'm not sure how to put that in a test.
I've changed the "some_date" field in author.sql to "some_Date" for this test,

Renaming it in the test seems strange, adding it is hard to do since I can only test for mysql,
so I'm leaving it like this.
6698b73
@greut
Collaborator

If you modified the SQL schema, why didn't you commit the file test/sql/mysql.sql too?

@NanneHuiges NanneHuiges Changed field name rom some_date to someDate
This is in addition to the test file from 6698b73
95af27a
@NanneHuiges

Well, as I tried to write, I didn't know what the best practice for writing tests is. I've no issues with committin it obviously, but I can imagine it isn't wanted/expected to get these changes to the test-sql in a pull request?.

I'll commit it later, but I'm new here @ github : would you want the commit in this pull request, and if so, can I add it in some? Or just commit it, so it's ok in my fork?

@NanneHuiges

commited here: NanneHuiges@95af27a , if you have further requests regarding what to do with it and/or time to help me get it sorted out as best as possible, it would be welcome :)

@NanneHuiges

Did you look into this? Any opinion maybe, even if you don't think its usefull in the code, I'm still curious what you think about this, or why it was chosen to work like it does?

@greut greut was assigned
@greut greut closed this
@greut
Collaborator

I've adapted the other adapters test too. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 14, 2012
  1. @NanneHuiges

    Changed assign_attribute so that fields with a different inflected na…

    NanneHuiges authored
    …me (e.g. capital letters) get casted too
    
    For instance: if you have a field called 'ThisIsADate' it would not become a datetime object.
    If the old array_key_exists check fails, a second check is done using the inflected name.
  2. @NanneHuiges

    Testing the 'datefield-with-capital' bug

    NanneHuiges authored
    Needs a datefield with a capital letter. I'm not sure how to put that in a test.
    I've changed the "some_date" field in author.sql to "some_Date" for this test,
    
    Renaming it in the test seems strange, adding it is hard to do since I can only test for mysql,
    so I'm leaving it like this.
Commits on Mar 15, 2012
  1. @NanneHuiges

    Changed field name rom some_date to someDate

    NanneHuiges authored
    This is in addition to the test file from 6698b73
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 4 deletions.
  1. +8 −3 lib/Model.php
  2. +19 −0 test/DateFormatTest.php
  3. +1 −1  test/sql/mysql.sql
View
11 lib/Model.php
@@ -436,10 +436,15 @@ public function __wakeup()
public function assign_attribute($name, $value)
{
$table = static::table();
-
- if (array_key_exists($name,$table->columns) && !is_object($value))
+ if (array_key_exists($name,$table->columns) && !is_object($value)){
$value = $table->columns[$name]->cast($value,static::connection());
-
+ }else{
+ $col = $table->get_column_by_inflected_name($name);
+ if (!is_null($col) && !is_object($value)){
+ $value = $col->cast($value,static::connection());
+ }
+ }
+
// convert php's \DateTime to ours
if ($value instanceof \DateTime)
$value = new DateTime($value->format('Y-m-d H:i:s T'));
View
19 test/DateFormatTest.php
@@ -0,0 +1,19 @@
+<?php
+include 'helpers/config.php';
+
+class DateFormatTest extends DatabaseTest
+{
+
+ public function test_datefield_gets_converted_to_ar_datetime()
+ {
+ //make sure first author has a date
+ $author = Author::first();
+ $author->some_date = new DateTime();
+ $author->save();
+
+ $author = Author::first();
+ $this->assert_is_a("ActiveRecord\\DateTime",$author->some_date);
+ }
+
+};
+?>
View
2  test/sql/mysql.sql
@@ -4,7 +4,7 @@ CREATE TABLE authors(
name VARCHAR(25) NOT NULL DEFAULT 'default_name',
updated_at datetime,
created_at datetime,
- some_date date,
+ some_Date date,
some_time time,
some_text text,
some_enum enum('a','b','c'),
Something went wrong with that request. Please try again.