使用 typeMap() 覆盖 Cakephp 3.1 数据类型
Override Cakephp 3.1 data type using typeMap()
当我有疑问时:
$jobs = $this->Jobs->find('all', [
'fields' => [
'id',
'existingdatetime',
'newdatetime' => '"2016-05-10 16:12:10"',
],
]);
existingdatetime作为DateTime对象返回,newdatetime作为字符串返回,这是有道理的,因为不存在的字段newdatetime没有数据定义。基于 Cakephp-3.x: How to change the data type of a selected alias? 我尝试了以下方法:
debug($jobs->typeMap());
$jobs->typeMap()->addDefaults([
'Jobs.newdatetime' => 'datetime',
'newdatetime' => 'datetime',
]);
debug($jobs->typeMap());
$results = $jobs->all();
debug($results);
乍一看,TypeMap 似乎已经成功更新,这里是旧版本:
/src/Controller/Admin/JobsController.php(第 97 行)
object(Cake\Database\TypeMap) {
[protected] _defaults => [
'Jobs.id' => 'integer',
'id' => 'integer',
'Jobs.existingdatetime' => 'datetime',
'existingdatetime' => 'datetime'
]
[protected] _types => []
}
调用 addDefaults() 后的新版本似乎数据类型更新得很好...
/src/Controller/Admin/JobsController.php(第 102 行)
object(Cake\Database\TypeMap) {
[protected] _defaults => [
'Jobs.id' => 'integer',
'id' => 'integer',
'Jobs.existingdatetime' => 'datetime',
'existingdatetime' => 'datetime',
'Jobs.newdatetime' => 'datetime'
'newdatetime' => 'datetime',
]
[protected] _types => []
}
然而结果仍然有 newdatetime 作为字符串:
/src/Controller/Admin/JobsController.php(第 106 行)
object(Cake\ORM\ResultSet) {
'query' => object(Cake\ORM\Query) {
'(help)' => 'This is a Query object, to get the results execute or iterate it.',
'sql' => 'SELECT Jobs.id AS `Jobs__id`, Jobs.existingdatetime AS `Jobs__existingdatetime`, "2016-05-10 16:12:10" AS `newdatetime` FROM jobs Jobs',
'params' => [],
'defaultTypes' => [
'Jobs.id' => 'integer',
'id' => 'integer',
'Jobs.existingdatetime' => 'datetime',
'existingdatetime' => 'datetime',
'Jobs.newdatetime' => 'datetime'
'newdatetime' => 'datetime',
],
'decorators' => (int) 0,
...snip...
}
'items' => [
(int) 0 => object(App\Model\Entity\Job) {
'id' => (int) 1,
'existingdatetime' => object(Cake\I18n\Time) {
'time' => '2016-04-27T03:21:32+1000',
'timezone' => 'Australia/Melbourne',
'fixedNowTime' => false
},
'newdatetime' => '2016-05-10 16:12:10',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Jobs'
}
]
}
我错过了什么?为什么我不能让我的额外字段成为日期时间?
请不要建议完全不同的方法,实际上,字段 newdatetime 是一个复杂的左内连接子查询,为了简单起见,我已经从这个问题中删除了它。
它不使用 TypeMap 函数,但它会将自定义字段转换为您需要的日期时间类型
$jobs = $this->Jobs->find()
->select([ 'id', 'existingdatetime', 'newdatetime' => '"2016-05-10 16:12:10"' ])
->decorateResults(function ($row) {
$row['newdatetime'] = new \Cake\I18n\Time($row['newdatetime']);
return $row;
});
您可以在此处查看更多相关信息:CakePHP 3 Decorate Results
当我有疑问时:
$jobs = $this->Jobs->find('all', [
'fields' => [
'id',
'existingdatetime',
'newdatetime' => '"2016-05-10 16:12:10"',
],
]);
existingdatetime作为DateTime对象返回,newdatetime作为字符串返回,这是有道理的,因为不存在的字段newdatetime没有数据定义。基于 Cakephp-3.x: How to change the data type of a selected alias? 我尝试了以下方法:
debug($jobs->typeMap());
$jobs->typeMap()->addDefaults([
'Jobs.newdatetime' => 'datetime',
'newdatetime' => 'datetime',
]);
debug($jobs->typeMap());
$results = $jobs->all();
debug($results);
乍一看,TypeMap 似乎已经成功更新,这里是旧版本:
/src/Controller/Admin/JobsController.php(第 97 行)
object(Cake\Database\TypeMap) {
[protected] _defaults => [
'Jobs.id' => 'integer',
'id' => 'integer',
'Jobs.existingdatetime' => 'datetime',
'existingdatetime' => 'datetime'
]
[protected] _types => []
}
调用 addDefaults() 后的新版本似乎数据类型更新得很好...
/src/Controller/Admin/JobsController.php(第 102 行)
object(Cake\Database\TypeMap) {
[protected] _defaults => [
'Jobs.id' => 'integer',
'id' => 'integer',
'Jobs.existingdatetime' => 'datetime',
'existingdatetime' => 'datetime',
'Jobs.newdatetime' => 'datetime'
'newdatetime' => 'datetime',
]
[protected] _types => []
}
然而结果仍然有 newdatetime 作为字符串:
/src/Controller/Admin/JobsController.php(第 106 行)
object(Cake\ORM\ResultSet) {
'query' => object(Cake\ORM\Query) {
'(help)' => 'This is a Query object, to get the results execute or iterate it.',
'sql' => 'SELECT Jobs.id AS `Jobs__id`, Jobs.existingdatetime AS `Jobs__existingdatetime`, "2016-05-10 16:12:10" AS `newdatetime` FROM jobs Jobs',
'params' => [],
'defaultTypes' => [
'Jobs.id' => 'integer',
'id' => 'integer',
'Jobs.existingdatetime' => 'datetime',
'existingdatetime' => 'datetime',
'Jobs.newdatetime' => 'datetime'
'newdatetime' => 'datetime',
],
'decorators' => (int) 0,
...snip...
}
'items' => [
(int) 0 => object(App\Model\Entity\Job) {
'id' => (int) 1,
'existingdatetime' => object(Cake\I18n\Time) {
'time' => '2016-04-27T03:21:32+1000',
'timezone' => 'Australia/Melbourne',
'fixedNowTime' => false
},
'newdatetime' => '2016-05-10 16:12:10',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Jobs'
}
]
}
我错过了什么?为什么我不能让我的额外字段成为日期时间?
请不要建议完全不同的方法,实际上,字段 newdatetime 是一个复杂的左内连接子查询,为了简单起见,我已经从这个问题中删除了它。
它不使用 TypeMap 函数,但它会将自定义字段转换为您需要的日期时间类型
$jobs = $this->Jobs->find()
->select([ 'id', 'existingdatetime', 'newdatetime' => '"2016-05-10 16:12:10"' ])
->decorateResults(function ($row) {
$row['newdatetime'] = new \Cake\I18n\Time($row['newdatetime']);
return $row;
});
您可以在此处查看更多相关信息:CakePHP 3 Decorate Results