Issue
I have a mutator:
protected function _setPurchaseTime($purchase_time) {
return Time($purchase_time);
}
This mutator gets run fine when I set the value like this:
$transaction->purchase_time = $this->request->data['purchase_time'];
The mutator correctly gets $purchase_time
. But when I try to set it like this:
$transaction = $this->Transactions->patchEntity($transaction, $this->request->data);
$purchase_time
is null. I can't figure out why this would be the case? The mutator gets run just fine, but the variable is null.
EDIT:
I should also add that purchase_time
is 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 extra data
This is the data going into 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 this is the data after patchEntity is 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 you can see, if you do the conversion of the Unix timestamp in the purchase_time
field, it does not equal the input. It equals the current time now, which is expected when passing in a NULL
value into Time()
.
Solution
The problem is that the DatetimeType
class in CakePHP expects the input in a normalized format. Either as an array (the form inputs) or as a string in ISO format.
Since you are providing an input in a localized format, you either need to enable the locale parser, send the input in the ISO format, transform the data before it is converted to datetime, or extend the DateTimeType
class to fit your needs.
One simple way of doing this is using the 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 is important to remember that data is not set to the entity if it cannot be correctly validated or if it cannot be "parsed" byt the Marshaller with the help of the Type classes. This is the reason your setter was not being called when using patchEntity.
Answered By - José Lorenzo Rodríguez
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.