Created
August 21, 2014 08:43
-
-
Save yuuan/2d9be95d0bf8131f727c to your computer and use it in GitHub Desktop.
CakePHPで `created` の代わりに `created_at` を使うようにするために AppModel を強引にいじってみた。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Application model for Cake. | |
* | |
* This file is application-wide model file. You can put all | |
* application-wide model-related methods here. | |
* | |
* @link http://cakephp.org CakePHP(tm) Project | |
* @package app.Model | |
* @since CakePHP(tm) v 0.2.9 | |
*/ | |
App::uses('Model', 'Model'); | |
/** | |
* Application model for Cake. | |
* | |
* Add your application-wide methods in the class below, your models | |
* will inherit them. | |
* | |
* @package app.Model | |
*/ | |
class AppModel extends Model { | |
/** | |
* 置き換えたいカラム名の連想配列 | |
* | |
* @var array | |
*/ | |
protected $fieldAliases = [ | |
'created' => 'created_at', | |
'updated' => 'updated_at', | |
'modified' => 'modified_at', | |
]; | |
public function getColumnType($column) { | |
if (isset($this->fieldAliases[$column])) { | |
$column = $this->fieldAliases[$column]; | |
} | |
return parent::getColumnType($column); | |
} | |
public function hasField($name, $checkVirtual = false) { | |
if (is_array($name)) { | |
foreach ($name as $n) { | |
if ($this->hasField($n, $checkVirtual)) { | |
return $n; | |
} | |
} | |
return false; | |
} | |
if (isset($this->fieldAliases[$name])) { | |
$name = $this->fieldAliases[$name]; | |
} | |
return parent::hasField($name, $checkVirtual); | |
} | |
public function set($one, $two = null) { | |
$data = parent::set($one, $two); | |
if (empty($data)) return $data; | |
foreach ($data as $modelName => $fieldSet) { | |
if (!is_array($fieldSet)) { | |
continue; | |
} | |
foreach ($this->fieldAliases as $fieldBefore => $fieldAfter) { | |
if (isset($this->data[$modelName][$fieldBefore])) { | |
$this->data[$modelName][$fieldAfter] = $this->data[$modelName][$fieldBefore]; | |
unset($this->data[$modelName][$fieldBefore]); | |
} | |
if (isset($data[$modelName][$fieldBefore])) { | |
$data[$modelName][$fieldAfter] = $data[$modelName][$fieldBefore]; | |
unset($data[$modelName][$fieldBefore]); | |
} | |
} | |
} | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment