无法设置日期时间字段CakePHP 3

时间:2015-07-10 02:06:26

标签: cakephp cakephp-3.0

我有一个改变者:

protected function _setPurchaseTime($purchase_time) {
    return Time($purchase_time);
}

当我设置这样的值时,这个mutator运行正常:

$transaction->purchase_time = $this->request->data['purchase_time'];

mutator正确获取$purchase_time。但是,当我尝试这样设置时:

$transaction = $this->Transactions->patchEntity($transaction, $this->request->data);

$purchase_time为空。我无法弄清楚为什么会这样? mutator运行得很好,但变量为null。

编辑:

我还应该补充说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()时,这是预期的。

0 个答案:

没有答案