gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:14:22 25 4
gpt4 key购买 nike

我正在尝试保存新创建的实体。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com