Skip to content
This repository

CTimeStampBehavior Issue 2145 #2147

Open
wants to merge 1 commit into from

8 participants

Ruslan Fadeev Carsten Brandt resurtm Maurizio Domba Cerin PrplHaz4 tom-- matthewvalimaki Carsten Schmitz
Carsten Brandt
Collaborator

This currently breaks BC, but it seems to be easy to fix that problem. Will check it in detail later.

resurtm resurtm commented on the diff
framework/zii/behaviors/CTimestampBehavior.php
((10 lines not shown))
75 81 'datetime'=>'NOW()',
76 82 'timestamp'=>'NOW()',
77 83 'date'=>'NOW()',
  84 + ),
  85 + 'oci'=>array(
  86 + 'datetime'=>'NOW()',
1
resurtm Collaborator
resurtm added a note

Oracle way:

SQL> select "created_at" from resurtm."orders" where "key1"=1;

created_at
---------------------------------------------------------------------------
09-MAR-13 12.15.13.143000 PM
09-MAR-13 12.15.13.143000 PM

SQL> update resurtm."orders" set "created_at" = now() where "key1" = 1;
update resurtm."orders" set "created_at" = now() where "key1" = 1
                                           *
ERROR at line 1:
ORA-00904: "NOW": invalid identifier


SQL> update resurtm."orders" set "created_at" = (select systimestamp from dual)
where "key1" = 1;

2 rows updated.

SQL> select "created_at" from resurtm."orders" where "key1"=1;

created_at
---------------------------------------------------------------------------
09-MAR-13 12.16.02.376000 PM
09-MAR-13 12.16.02.376000 PM

SQL> commit;

Commit complete.

SQL> select "created_at" from resurtm."orders" where "key1"=1;

created_at
---------------------------------------------------------------------------
09-MAR-13 12.16.02.376000 PM
09-MAR-13 12.16.02.376000 PM

SQL>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
resurtm
Collaborator

There is CTimestampBehaviorTest.php. What do you think about enhancing it too? Can you handle it? :-)

resurtm resurtm commented on the diff
CHANGELOG
@@ -24,6 +24,7 @@ Version 1.1.14 work in progress
24 24 - Enh #1948: Tidy up and improve html5 input support in CHtml and CActiveForm (phpnode)
25 25 - Enh #1977: CFormatter::normalizeDateValue() now is protected instead of private to enable child classes to override it (etienneq)
26 26 - Enh #2038: CFormatter::formatNtext() method can replace newlines with `<p></p>` not just with `<br />` as it was before (resurtm)
  27 +- Enh #2145: CTimestampBehavior::$map timestamp functions now support database types
1
resurtm Collaborator
resurtm added a note

You've forgot your nickname. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Maurizio Domba Cerin
Collaborator

Because of that there is the property $timestampExpression - http://www.yiiframework.com/doc/api/1.1/CTimestampBehavior#timestampExpression-detail

resurtm
Collaborator

@mdomba, yes, i understand that, but Yii has limited set of supported RDBMSes, so why not to implement this from the core side? It would give additional comfort to the end users. :-)

PrplHaz4

It would be nice if there was also a map that facilitated storing timestamps in UTC as many applications are now utilized across timezones. I realize this is doable through timestampExpression, but would be nice to have the option handled by the framework in order to be more predictable.

tom--
tom-- commented

I agree with @PrplHaz4. In MySQL, for example, NOW() returns the time in the current DB connection's zone, which will be the DB server's zone if the client hasn't specified otherwise. The DB server's zone will probably be whatever the server's OS thinks is its zone, assuming the DB admin hasn't configured otherwise. And the OS zone may or may not have some value that makes sense to the application. There's a lot of scope for all this to not work out for the best. I've no idea what to expect on other DBMSs.

Using UTC for all dates, times and datetimes in the DB is a simple way of avoiding all this.

matthewvalimaki

Having 'NOW()' as the only option and instructing to use $timestampExpression is short sighted. As CTimeStampBehavior is an extension to the active record, it should not be limited to one particular method.

Also the official guide specifically uses SQLite. Either supporting SQLite or specifically writing out instructions into the guide needs to be done.

In 2011 someone contributed a patch to supply support for SQLite https://code.google.com/p/yii/issues/detail?id=2578

ggirtsou ggirtsou referenced this pull request from a commit in ggirtsou/yii
ggirtsou ggirtsou Updated CHtml.php to add maxlength support to all HTML5 fields
As requested by @BrunoLL on Issue #2147
bd30dff
Carsten Schmitz

I strongly support this patch and I ask for inclusion. I think more patches contributing the missing support for other DBMSs will follow soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 23, 2013
Ruslan Fadeev magefad Enh #2145: CTimestampBehavior::$map timestamp functions now support d…
…atabase types
eb5afa2
This page is out of date. Refresh to see the latest.
1  CHANGELOG
@@ -24,6 +24,7 @@ Version 1.1.14 work in progress
24 24 - Enh #1948: Tidy up and improve html5 input support in CHtml and CActiveForm (phpnode)
25 25 - Enh #1977: CFormatter::normalizeDateValue() now is protected instead of private to enable child classes to override it (etienneq)
26 26 - Enh #2038: CFormatter::formatNtext() method can replace newlines with `<p></p>` not just with `<br />` as it was before (resurtm)
  27 +- Enh #2145: CTimestampBehavior::$map timestamp functions now support database types
27 28 - Chg #645: CDbConnection now throws CDbException when failed to open DB connection instead of failing with a warning (kidol, eirikhm, samdark, cebe)
28 29 - Chg #1891: Changed order of methods in models generated by Gii and yiic, added better description of search method (hijarian, samdark)
29 30 - New #575: Yii registering at Packagist, added composer info file (schmunk42)
27 framework/zii/behaviors/CTimestampBehavior.php
@@ -72,9 +72,31 @@ class CTimestampBehavior extends CActiveRecordBehavior {
72 72 * @var array Maps column types to database method
73 73 */
74 74 protected static $map = array(
  75 + 'mssql'=>array(
  76 + 'datetime'=>'GETDATE()',
  77 + 'timestamp'=>'GETDATE()',
  78 + 'date'=>'GETDATE()',
  79 + ),
  80 + 'mysql'=>array(
75 81 'datetime'=>'NOW()',
76 82 'timestamp'=>'NOW()',
77 83 'date'=>'NOW()',
  84 + ),
  85 + 'oci'=>array(
  86 + 'datetime'=>'NOW()',
  87 + 'timestamp'=>'NOW()',
  88 + 'date'=>'NOW()',
  89 + ),
  90 + 'pgsql'=>array(
  91 + 'datetime'=>'NOW()',
  92 + 'timestamp'=>'NOW()',
  93 + 'date'=>'NOW()',
  94 + ),
  95 + 'sqlite'=>array(
  96 + 'datetime'=>'datetime(\'now\')',
  97 + 'timestamp'=>'datetime(\'now\')',
  98 + 'date'=>'date(\'now\')',
  99 + )
78 100 );
79 101
80 102 /**
@@ -109,12 +131,13 @@ protected function getTimestampByAttribute($attribute) {
109 131 }
110 132
111 133 /**
112   - * Returns the approprate timestamp depending on $columnType
  134 + * Returns the appropriate timestamp depending on $columnType
113 135 *
114 136 * @param string $columnType $columnType
115 137 * @return mixed timestamp (eg unix timestamp or a mysql function)
116 138 */
117 139 protected function getTimestampByColumnType($columnType) {
118   - return isset(self::$map[$columnType]) ? new CDbExpression(self::$map[$columnType]) : time();
  140 + $driverName = Yii::app()->db->getDriverName();
  141 + return isset(self::$map[$driverName][$columnType]) ? new CDbExpression(self::$map[$driverName][$columnType]) : time();
119 142 }
120 143 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.