Cake\ORM\Marshaller::_mergeAssociation(): Argument #1 ($original) must be of type Cake\Datasource\EntityInterface|array|null, int given,

Tried to post more details. Forum response was: An error occurred: Sorry, new users can only mention 2 users in a post.

Version: 5.2.5

Same code works in Version: 4.5.8 This is not a migration, clean install.
I have stripped down the controller and model for trouble shooting. My confusion is why is cake trying to _mergeAssociation.
Same error when getting the entity by primary key $company = $this->Companies->get($id);
If I remove the select list from edit form, entity posts successfully.
If I remove the hasOne Association, entity posts successfully.
But the return view errors because of the missing association.

Full error:

2025-07-10 12:55:57 error: [TypeError] Cake\ORM\Marshaller::_mergeAssociation(): Argument #1 ($original) must be of type Cake\Datasource\EntityInterface|array|null, 
int given, called in /var/www/vhosts/dpg3.schillers.com/app/vendor/cakephp/cakephp/src/ORM/Marshaller.php on line 123 in /var/www/vhosts/dpg3.schillers.com/app/vendor/cakephp/cakephp/src/ORM/Marshaller.php on line 752
Stack Trace:
- 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
- [main]:

Don’t upgrade from 4.5 to 5.2 directly.

Go to 4.6, then 5.0, then 5.1 and then 5.2

Kevin,
Thank you for the quick reply.
Not a migration. Clean install v5.2.5

just looking at you callstack tells me this is not a clean install.

the default cakephp/app repo doesn’t have AuthenticationMiddleware enabled by default.

Please copy over what you have in your APP/Controller/CompaniesController.php Line 1187

That came from a plugin. CakePHP 5.x User Management Premium Plugin Version 5.0.x from https://developers.ektanjali.com/docs/umpremium/version5.0/index.html

$this->Companies->patchEntity($company, $this->request->getData());

look at what is inside your request data.

It contains data, which the ORM can’t handle.

My guess would be, that you need to make adjustments in your template (which is responsibie for the structure of data you get via $this->Form->control() etc.)

Added some logging to the ORM/Marshaller and the form data is getting there.
Added a contain on the find

      $company = $this->Companies
         ->find()
         ->select(['Companies.id', 'Companies.name', 'Companies.phone', 'Companies.fax', 'Companies.street', 'Companies.street_2', 'Companies.city', 
            'Companies.state', 'Companies.zip', 'Companies.cp1_account', 'Companies.company_type_id',])
         ->contain([
            'CompanyType' => [
               'fields' => [
                  'CompanyType.id',
                  'CompanyType.secondary_id',
                  'CompanyType.a10n',
                  'CompanyType.description',
                  'CompanyType.flag',
               ]
         ]])
         ->where(['Companies.id' => $id])
         ->first();

no error, but field is not updated on patchEntity. This is the initial issue.

$formData: Array

(
    [id] => 1523
    [name] => Ricandste, LLC
    [phone] => 314-968-7777
    [fax] => 999-999-8888
    [street] => 960 Hanna Northeast
    [street_2] => 
    [city] => Manchester
    [state] => MO
    [zip] => 63021-1111
    [cp1_account] => 2107523
    [company_type_id] => 1
)

App\Model\Entity\Company Object

(
    [id] => 1523
    [name] => Ricandste, LLC
    [phone] => 314-968-7777
    [fax] => 999-999-8888
    [street] => 960 Hanna Northeast
    [street_2] => 
    [city] => Manchester
    [state] => MO
    [zip] => 63021-1111
    [cp1_account] => 2107523
    [company_type_id] => 
    [[new]] => 
    [[accessible]] => Array
        (
            [*] => 1
        )

    [[dirty]] => Array
        (
            [company_type_id] => 1
        )

    [[original]] => Array
        (
            [company_type_id] => App\Model\Entity\DataGroupDetail Object
                (
                    [id] => 1
                    [secondary_id] => 1
                    [a10n] => C
                    [description] => Customer
                    [flag] => Y

My concern now is that I am trying to do something with my model that is breaking cake.
Should I switch my focus their and provide details in that area.
Kevin, I really do appreciate your input!

I don’t understand why you are making such a complicated query if you could just do

$company = $this->Companies->get($id);
$data = $this->request->getData();
$company = $this->Companies->patchEntity($company, $data);
$this->Companies->save($company);

But cake shouldn’t break if you want to overwrite a foreign key field with a new value so I’d have to try and make that somewhat reproducable.

1 Like

I gave up. Deleted the controller, table and entity. Re baked and everything now works.
Still can’t find the gremlin.

Kevin, Thank you so much for your effort. If I discover the gremlin, I will update this post.

Found my gremlin.

I had three hasOne associations that when I appended the “_key” in the table column, table and the form, all started working.
Example: Changed “tax_cod” to “tax_cod_key”

No idea why this logic worked in v4.5.8 and not in v5.2.5. Naming conventions???

  $this->hasOne('TaxCod', [
        'className' => 'DataGroupDetails'
     ])
     ->setForeignKey('secondary_key')
     ->setBindingKey('tax_cod')
     ->setProperty('tax_cod')
     ->setConditions(['TaxCod.data_group_id' => 7])
     ->setDependent(false);

To

  $this->hasOne('TaxCod', [
        'className' => 'DataGroupDetails'
     ])
     ->setForeignKey('secondary_key')
     ->setBindingKey('tax_cod_key')
     ->setProperty('tax_cod')
     ->setConditions(['TaxCod.data_group_id' => 7])
     ->setDependent(false);

Different association that worked from the start

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

The duplicate naming is the problem. On the entity, the field tax_code would map to the tax_code db column but then you used the same name for the association property, so it would get overwritten with the associated entity instance.

If you want to keep the db column same then you need to use a different association property name.

1 Like