我有一个突变:无法设置日期时间字段CakePHP的3
protected function _setPurchaseTime($purchase_time) {
return Time($purchase_time);
}
当我这样设置值这个突变被运行良好:
$transaction->purchase_time = $this->request->data['purchase_time'];
的突变正确得到$purchase_time
。但是,当我试图设置它是这样的:
$transaction = $this->Transactions->patchEntity($transaction, $this->request->data);
$purchase_time
为空。我无法弄清楚为什么会出现这种情况? mutator运行得很好,但变量为空。
编辑:
我还要补充一点,purchase_time
可访问:
protected $_accessible = [
'ticker' => TRUE,
'name' => TRUE,
'market' => TRUE,
'transaction_type' => TRUE,
'price' => TRUE,
'currency' => TRUE,
'commission' => TRUE,
'shares' => TRUE,
'purchase_time' => TRUE
];
编辑:增加了额外的数据
这是进入patchEntity数据:
[
'ticker_label' => 'AAPL (Apple Inc.)',
'ticker' => 'AAPL',
'currency' => 'USD',
'market' => 'NASDAQ',
'transaction_type' => 'Buy',
'price' => '10',
'commission' => '10',
'shares' => '10',
'date' => 'Yesterday',
'purchase_time' => 'July 12, 2015 12:00',
'time' => '12:00',
'name' => 'Apple Inc.'
]
这是patchEntity运行后的数据:
object(App\Model\Entity\Transaction) {
'portfolio_id' => '43',
'ticker' => 'AAPL',
'currency' => 'USD',
'market' => 'NASDAQ',
'transaction_type' => 'Buy',
'price' => (float) 10,
'commission' => (float) 10,
'shares' => (float) 10,
'purchase_time' => (int) 1436803512,
'name' => 'Apple Inc.',
'[new]' => true,
'[accessible]' => [
'ticker' => true,
'name' => true,
'market' => true,
'transaction_type' => true,
'price' => true,
'currency' => true,
'commission' => true,
'shares' => true,
'purchase_time' => true
],
'[dirty]' => [
'portfolio_id' => true,
'ticker' => true,
'currency' => true,
'market' => true,
'transaction_type' => true,
'price' => true,
'commission' => true,
'shares' => true,
'purchase_time' => true,
'name' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Transactions'
}
正如您所看到的,如果您在purchase_time
字段中对Unix时间戳记进行了转换,则它不等于输入。它等于现在的时间,预计将NULL
的值传递到Time()
。
您可能有验证错误 –
@JoséLorenzo我现在没有验证,还没有设置任何,因为我想在我添加验证之前首先得到这个工作。 –
修补程序实体后调试($ transaction)的结果是什么? –