Unable to set date time field CakePHP 3 -


i have mutator:

protected function _setpurchasetime($purchase_time) {     return time($purchase_time); } 

this mutator gets run fine when set value this:

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

the mutator correctly gets $purchase_time. when try set this:

$transaction = $this->transactions->patchentity($transaction, $this->request->data); 

$purchase_time null. can't figure out why case? mutator gets run fine, variable null.

edit:

i should add purchase_time accessible:

protected $_accessible = [  'ticker' => true,  'name' => true,  'market' => true,  'transaction_type' => true,  'price' => true,  'currency' => true,  'commission' => true,  'shares' => true,  'purchase_time' => true ]; 

edit: added data

this data going 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.' ] 

and data after patchentity run:

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' } 

as can see, if conversion of unix timestamp in purchase_time field, not equal input. equals current time now, expected when passing in null value time().

the problem datetimetype class in cakephp expects input in normalized format. either array (the form inputs) or string in iso format.

since providing input in localized format, either need enable locale parser, send input in iso format, transform data before converted datetime, or extend datetimetype class fit needs.

one simple way of doing using model.beforemarshal event:

$table->eventmanager()->on('model.beforemarshal', function ($event, $data) {     if (!empty($data['the_time_field']) {         $data['the_time_field'] = new time($data['the_time_field'])     } }); 

it important remember data not set entity if cannot correctly validated or if cannot "parsed" byt marshaller of type classes. reason setter not being called when using patchentity.


Comments