php - CakePHP 3新实体保存失败,没有错误

标签 php mysql cakephp

我正在尝试保存新创建的实体。 Save 返回 false,但我看不到任何验证错误或 SQL 错误,也没有抛出异常。

实体对象是在表对象上使用 newEntity() 创建的,然后进行填充。当我调用 save() 时,它返回 false。这是 save() 失败后实体对象的调试输出:

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

当我尝试在 phpMyAdmin 中手动插入这些值时,效果很好。

这是表格:

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;

我尝试打开查询日志记录,但只收到一堆以下条目:

2017-11-07 01:13:29 Debug: duration=0 rows=0 ROLLBACK
2017-11-07 01:13:29 Debug: duration=0 rows=0 BEGIN

此代码是从 Shell 类运行的。但在同一个类的另一个函数中,非常相似的保存工作正常,所以我认为这不是问题。

表和实体类不包含任何逻辑或回调。

感谢您对此提供的任何帮助!

编辑:这就是问题 - 我将实体保存到不同的模型。这不应该抛出异常吗?

$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 );
}

最佳答案

问题是我正在创建一个 TickerValue 实体,但实际上将其保存到 Currencies 模型中。

在我看来,这应该会导致抛出异常,但也许 Cakephp 社区中有人比我更适合决定是否应该将其报告为错误。

关于php - CakePHP 3新实体保存失败,没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47148645/

相关文章:

php - 这看起来像密码的强盐吗

php - 正确代码中的命令不同步

php - 从 Wordpress 用户元表中获取数据

php - cakephp中的数据解码问题

php - 如何将 html 选项卡包含到分页器 cakephp 中

PHP跟踪像素数据库插入

php - 在php中显示html代码

php - 如何防止在不同的 apache 虚拟主机之间共享 PHP session ?

php - 根据时区获取今天的记录

mysql - 处理 Cakephp 中的数据库连接错误