You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CakePHP 5.2.5中_mergeAssociation类型错误排查求助

CakePHP 5.2.5 类型错误排查求助

错误详情

完整错误:

error: [TypeError] Cake\ORM\Marshaller::_mergeAssociation(): 参数 #1 ($original) 必须为 Cake\Datasource\EntityInterface|array|null 类型,传入的是 int,调用位置为 /var/www/vhosts/dpg3.schillers.com/app/vendor/cakephp/cakephp/src/ORM/Marshaller.php 第123行,错误发生在该文件第752行

调用栈:

  • CORE/src/ORM/Marshaller.php:123
  • CORE/src/ORM/Marshaller.php:603
  • CORE/src/ORM/Table.php:3056
  • APP/Controller/CompaniesController.php:1187
  • CORE/src/Controller/Controller.php:505
  • CORE/src/Controller/ControllerFactory.php:166
  • CORE/src/Controller/ControllerFactory.php:141
  • CORE/src/Http/BaseApplication.php:362
  • CORE/src/Http/Runner.php:86
  • CORE/src/Http/Middleware/CsrfProtectionMiddleware.php:169
  • CORE/src/Http/Runner.php:82
  • ROOT/vendor/cakephp/authentication/src/Middleware/AuthenticationMiddleware.php:106
  • CORE/src/Http/Runner.php:82
  • CORE/src/Http/Middleware/BodyParserMiddleware.php:162
  • CORE/src/Http/Runner.php:82
  • CORE/src/Routing/Middleware/RoutingMiddleware.php:117
  • CORE/src/Http/Runner.php:82
  • CORE/src/Routing/Middleware/AssetMiddleware.php:70
  • CORE/src/Http/Runner.php:82
  • CORE/src/Error/Middleware/ErrorHandlerMiddleware.php:115
  • CORE/src/Http/Runner.php:82
  • ROOT/vendor/cakephp/debug_kit/src/Middleware/DebugKitMiddleware.php:60
  • CORE/src/Http/Runner.php:82
  • CORE/src/Http/Runner.php:60
  • CORE/src/Http/Server.php:104
  • ROOT/webroot/index.php:37

环境与现象

  • 框架版本:5.2.5(全新安装,非迁移项目)
  • 相同代码在CakePHP 4.5.8中可正常运行
  • 测试现象:
    • 移除编辑表单中的company_type_id下拉选择框后,实体可成功提交
    • 移除表模型中的hasOne CompanyType关联并保留表单下拉框,实体也可成功提交
    • 移除关联后视图会因缺少关联报错
    • 直接通过主键获取实体时也触发相同错误:$company = $this->Companies->get($id);

已精简控制器和模型代码用于排查,目前困惑的是CakePHP为何会调用_mergeAssociation方法,还需要检查哪些内容?

代码片段

控制器代码(CompaniesController.php)

public function edit($id = null)
{
    $company = $this->Companies
        ->find()
        ->select(['id', 'name', 'company_type_id', 'phone', 'fax', 'cp1_account'])
        ->where(['id' => $id])
        ->first();
    
    if ($this->request->is(['patch', 'post', 'put'])) {
        $this->Companies->patchEntity($company, $this->request->getData());
        if ($this->Companies->save($company)) {
            $this->Flash->success($company->name . ' has been saved');
            return $this->redirect(['action' => 'index']);
        }
    }
    $this->set('company', $company);
}

编辑表单代码

<div class="card">
    <div class="card-header bg-black mb-2 pt-1">
        card header
    </div>
    <div class="card-body">
        <?php
        echo $this->Form->create($company, [
            'novalidate' => true
        ]);
        echo $this->Form->control('name', ['style' => 'width:100%;']);
        echo $this->Form->control('phone', ['type' => 'text', 'style' => 'width:100%;']);
        echo $this->Form->control('fax', ['style' => 'width:100%;']);
        echo $this->Form->control('cp1_account', ['style' => 'width:100%;']);
        echo $this->Form->select('company_type_id', [
            '1' => 'Value 1',
            '2' => 'Value 2',
            '3' => 'Value 3'
        ]);
        echo $this->Form->button(__('Add'));
        echo $this->Form->end();
        ?>
    </div>     
</div>

表模型代码(CompaniesTable.php)

declare(strict_types=1);

namespace App\Model\Table;

use Cake\ORM\Table;

/**
 * Companies Model
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class CompaniesTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('companies');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasOne('CompanyType', [
                'className' => 'DataGroupDetails'
            ])
            ->setForeignKey('secondary_id')
            ->setBindingKey('company_type_id')
            ->setProperty('company_type_id')
            ->setConditions(['CompanyType.data_group_id' => 1])
            ->setDependent(false);
    }
}

内容的提问来源于stack exchange,提问作者smoeckel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 17:37:06