cakephp - 似乎无法在 cakephp 3.x 中保存数据
问题描述
所以我想将数据数组保存为我的数据库中的测试。
我的保存代码在 ResultLog.php
<?php
namespace Alis\Mall\Alpen\Domain\Log;
use Alis\MultiDbConnection\ClientDbConnectionManager;
use Cake\ORM\TableRegistry;
class ResultLog
{
private $MallExportResultLog;
public function __construct()
{
$MallExportResultLog = TableRegistry::get('MallExportResultLog');
}
/**
*/
public function save()
{
$MallExportResultLog->setConnection(ClientDbConnectionManager::getInstance()->getConnection());
$data = [
'stream_name' => "test",
'stream_version' => 1,
'event_type' => "test",
'data' => "test"
];
$entity = $MallExportResultLog->newEntity($data);
if (! $MallExportResultLog->save($entity)) {
throw new \Exception($entity->errors());
}
}
}
现在我尝试在 ExportAllProductsShell.php 中调用它,$resultlog = new ResultLog();
但它似乎不起作用。
这是我的做法。
<?php
namespace App\Shell;
use Cake\Console\Shell;
use Alis\Mall\Alpen\UseCase\ExportAllProducts;
use Alis\Mall\Alpen\Domain\Log\ResultLog;
/**
* ExportAllProductsShell shell command.
*/
class ExportAllProductsShell extends Shell
{
public function main()
{
$this->out('Start Execute.');
$export = new ExportAllProducts();
$export->export();
$resultlog = new ResultLog();
$this->out('Finish Execute.');
}
}
ExportAllProducts() 执行,但 ResultLog() 不能。
有什么不对或缺少什么?
解决方案
从您提供的代码片段中,您永远不会调用$resultlog->save()
. 如果错误地省略了它,那么您应该检查您的实体不包含任何会阻止保存的验证错误。
debug($entity->getErrors());
将让您查看实体中的任何验证错误。
推荐阅读
- postgresql - debezium 连接器的多个复制槽
- r - 整洁数据 r 变量的总和子集
- firebase - 如何为任何文档更新添加firebase触发器?
- r - 基于相同组行的新列
- haskell - 带有 RValue LValue 问题的无标签最终 DSL
- javascript - 如何使用 javascript 选择/隐藏类型为“text/html”的对象内的元素
- reactjs - 错误 renderIntoDocument 不是 react-testing-library 的函数
- php - 错误:该属性……也没有任何一种方法……不存在并且具有公共访问权限
- windows - 如何在本地运行 Meteor.js 但在 Windows 中将 mongo 连接到 mongodb.com
- php - 如何获取 URL 参数的第一个单词