Moved date formatting fully into the AppModel, automatically determining the necessary fields from the schema.
git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@233 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -40,7 +40,7 @@ class AppModel extends Model {
|
|||||||
|
|
||||||
var $actsAs = array('Containable', 'Linkable');
|
var $actsAs = array('Containable', 'Linkable');
|
||||||
var $useNullForEmpty = true;
|
var $useNullForEmpty = true;
|
||||||
var $dateFields = array();
|
var $formatDateFields = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Enum Values
|
* Get Enum Values
|
||||||
@@ -151,21 +151,38 @@ class AppModel extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function beforeSave() {
|
function beforeSave() {
|
||||||
|
/* pr(array('class' => $this->name, */
|
||||||
|
/* 'alias' => $this->alias, */
|
||||||
|
/* 'function' => 'AppModel::beforeSave')); */
|
||||||
|
|
||||||
// Replace all empty strings with NULL.
|
// Replace all empty strings with NULL.
|
||||||
// If a particular model doesn't like this, they'll have to
|
// If a particular model doesn't like this, they'll have to
|
||||||
// override the behavior, or set useNullForEmpty to false.
|
// override the behavior, or set useNullForEmpty to false.
|
||||||
if ($this->useNullForEmpty)
|
if ($this->useNullForEmpty)
|
||||||
$this->recursive_array_replace("/^\s*$/", null, $this->data);
|
$this->recursive_array_replace("/^\s*$/", null, $this->data);
|
||||||
|
|
||||||
foreach ($this->dateFields AS $field) {
|
if ($this->formatDateFields) {
|
||||||
/* if(isset($this->data['Transaction']['stamp']) && */
|
$alias = $this->alias;
|
||||||
/* $this->data['Transaction']['stamp'] !== 'CURRENT_TIMESTAMP') { */
|
|
||||||
/* $this->data['Transaction']['stamp'] = */
|
foreach ($this->_schema AS $field => $info) {
|
||||||
/* $this->dateFormatBeforeSave($this->data['Transaction']['stamp']); */
|
if ($info['type'] == 'date' || $info['type'] == 'timestamp') {
|
||||||
/* } */
|
if (isset($this->data[$alias][$field])) {
|
||||||
/* else { */
|
/* pr("Fix Date for '$alias'.'$field'; current value = " . */
|
||||||
/* $this->data['Transaction']['stamp'] = null; */
|
/* "'{$this->data[$alias][$field]}'"); */
|
||||||
|
if ($this->data[$alias]['stamp'] === 'CURRENT_TIMESTAMP')
|
||||||
|
// Seems CakePHP is broken for the default timestamp.
|
||||||
|
// It tries to automagically set the value to CURRENT_TIMESTAMP
|
||||||
|
// which is wholly rejected by MySQL. Just put it back to NULL
|
||||||
|
// and let the SQL engine deal with the defaults... it's not
|
||||||
|
// Cake's place to do this anyway :-/
|
||||||
|
$this->data[$alias][$field] = null;
|
||||||
|
else
|
||||||
|
$this->data[$alias][$field] =
|
||||||
|
$this->dateFormatBeforeSave($this->data[$alias][$field]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,21 +34,6 @@ class Lease extends AppModel {
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
function beforeSave() {
|
|
||||||
foreach (array('lease',
|
|
||||||
'movein', 'movein_planned',
|
|
||||||
'moveout', 'moveout_planned',
|
|
||||||
'notice_givein', 'notice_received',
|
|
||||||
'close', 'next_rent') AS $dtfield) {
|
|
||||||
$dtfield .= '_date';
|
|
||||||
if(isset($this->data['Lease'][$dtfield]))
|
|
||||||
$this->data['Lease'][$dtfield] =
|
|
||||||
$this->dateFormatBeforeSave($this->data['Lease'][$dtfield]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
**************************************************************************
|
**************************************************************************
|
||||||
**************************************************************************
|
**************************************************************************
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
class Transaction extends AppModel {
|
class Transaction extends AppModel {
|
||||||
|
|
||||||
var $name = 'Transaction';
|
var $name = 'Transaction';
|
||||||
/* var $validate = array( */
|
|
||||||
/* 'stamp' => array('date') */
|
var $validate = array(
|
||||||
/* ); */
|
'stamp' => array('date')
|
||||||
|
);
|
||||||
|
|
||||||
var $belongsTo = array(
|
var $belongsTo = array(
|
||||||
);
|
);
|
||||||
@@ -13,20 +14,5 @@ class Transaction extends AppModel {
|
|||||||
'LedgerEntry',
|
'LedgerEntry',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
function beforeSave() {
|
|
||||||
|
|
||||||
if(isset($this->data['Transaction']['stamp']) &&
|
|
||||||
$this->data['Transaction']['stamp'] !== 'CURRENT_TIMESTAMP') {
|
|
||||||
$this->data['Transaction']['stamp'] =
|
|
||||||
$this->dateFormatBeforeSave($this->data['Transaction']['stamp']);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$this->data['Transaction']['stamp'] = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return parent::beforeSave();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user