首页 > 解决方案 > 似乎无法在 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() 不能。

有什么不对或缺少什么?

标签: cakephploggingcakephp-3.x

解决方案


从您提供的代码片段中,您永远不会调用$resultlog->save(). 如果错误地省略了它,那么您应该检查您的实体不包含任何会阻止保存的验证错误。

debug($entity->getErrors());

将让您查看实体中的任何验证错误。


推荐阅读