0
votes

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 );
}
1
include your code - Beginner
Can you also include the controller which you are using to save the data? - Aarrbee
Hey guys, just added the code. Didn't include it originally because there's not much going on but hopefully it'll make things clearer! Like I mentioned I'm using a Shell class rather than a normal controller, but doesn't seem like that's the issue. - Simon
Oops, there it is, I'm saving to the wrong model! Isn't it weird that this isn't flagged as any kind of error or exception? - Simon

1 Answers

0
votes

The problem was that I was creating a TickerValue entity but actually saving it to the Currencies model.

It seems to me that this should result in an exception being thrown, but perhaps there's someone in the Cakephp community better placed than I to make the call on whether this should be reported as a bug.