Skip to content

Commit

Permalink
Enh yiisoft#2145: CTimestampBehavior::$map timestamp functions now su…
Browse files Browse the repository at this point in the history
…pport database types
  • Loading branch information
magefad committed Feb 23, 2013
1 parent 19e9f34 commit eb5afa2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -24,6 +24,7 @@ Version 1.1.14 work in progress
- Enh #1948: Tidy up and improve html5 input support in CHtml and CActiveForm (phpnode)
- Enh #1977: CFormatter::normalizeDateValue() now is protected instead of private to enable child classes to override it (etienneq)
- Enh #2038: CFormatter::formatNtext() method can replace newlines with `<p></p>` not just with `<br />` as it was before (resurtm)
- Enh #2145: CTimestampBehavior::$map timestamp functions now support database types
- Chg #645: CDbConnection now throws CDbException when failed to open DB connection instead of failing with a warning (kidol, eirikhm, samdark, cebe)
- Chg #1891: Changed order of methods in models generated by Gii and yiic, added better description of search method (hijarian, samdark)
- New #575: Yii registering at Packagist, added composer info file (schmunk42)
Expand Down
27 changes: 25 additions & 2 deletions framework/zii/behaviors/CTimestampBehavior.php
Expand Up @@ -72,9 +72,31 @@ class CTimestampBehavior extends CActiveRecordBehavior {
* @var array Maps column types to database method
*/
protected static $map = array(
'mssql'=>array(
'datetime'=>'GETDATE()',
'timestamp'=>'GETDATE()',
'date'=>'GETDATE()',
),
'mysql'=>array(
'datetime'=>'NOW()',
'timestamp'=>'NOW()',
'date'=>'NOW()',
),
'oci'=>array(
'datetime'=>'NOW()',
'timestamp'=>'NOW()',
'date'=>'NOW()',
),
'pgsql'=>array(
'datetime'=>'NOW()',
'timestamp'=>'NOW()',
'date'=>'NOW()',
),
'sqlite'=>array(
'datetime'=>'datetime(\'now\')',
'timestamp'=>'datetime(\'now\')',
'date'=>'date(\'now\')',
)
);

/**
Expand Down Expand Up @@ -109,12 +131,13 @@ protected function getTimestampByAttribute($attribute) {
}

/**
* Returns the approprate timestamp depending on $columnType
* Returns the appropriate timestamp depending on $columnType
*
* @param string $columnType $columnType
* @return mixed timestamp (eg unix timestamp or a mysql function)
*/
protected function getTimestampByColumnType($columnType) {
return isset(self::$map[$columnType]) ? new CDbExpression(self::$map[$columnType]) : time();
$driverName = Yii::app()->db->getDriverName();
return isset(self::$map[$driverName][$columnType]) ? new CDbExpression(self::$map[$driverName][$columnType]) : time();
}
}

0 comments on commit eb5afa2

Please sign in to comment.