I'm trying to save a newly created entity. Save returns false but I can't see any validation errors or SQL errors and no exception is thrown.
The entity object is created using newEntity() on the table object and then populated. When I then call save() it returns false. This is the debug output of the entity object after save() fails:
object(App\Model\Entity\TickerValue) {
'from_currency_id' => (int) 1,
'to_currency_id' => (int) 1228,
'created' => (int) 1509594561,
'value' => (float) 0.00726931,
'[new]' => true,
'[accessible]' => [
'*' => false
],
'[dirty]' => [
'from_currency_id' => true,
'to_currency_id' => true,
'created' => true,
'value' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'TickerValues'
}
When I try inserting these values manually in phpMyAdmin it works fine.
This is the table:
CREATE TABLE 'ticker_values' (
'from_currency_id' int(11) NOT NULL,
'to_currency_id' int(11) NOT NULL,
'created' int(11) NOT NULL,
'value' decimal(24,8) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I tried turning on query logging and am only getting a bunch of these entries:
2017-11-07 01:13:29 Debug: duration=0 rows=0 ROLLBACK
2017-11-07 01:13:29 Debug: duration=0 rows=0 BEGIN
This code is run from a Shell class. But in another function in the same class a very similar save works fine, so I don't think this is the issue.
The Table and Entity classes don't contain any logic or callbacks.
Thanks for any help with this!
EDIT: This was the problem - I was saving the entity to a different model. Shouldn't this throw an exception??
$priceFieldName = 'price_'.strtolower($fromCurrency->symbol);
$tickerValues = TableRegistry::get('TickerValues');
$newValue = $tickerValues->newEntity();
$newValue->from_currency_id = intval($fromCurrency->id);
$newValue->to_currency_id = intval($toCurrency->id);
$newValue->created = intval($tickerItem->last_updated);
$newValue->value = (float)$tickerItem->$priceFieldName;
if( !$this->currencies->save( $newValue ) ) {
debug( $newValue );
}