Virtual associations, multiple asssociations to an entity

Using cakephp 4.5.6 on php8.2 in my table class I have :

$this->belongsTo('CertificateBackgrounds', [
       	'className' => 'Files',
       	'foreignKey' => 'certificate_back_id',
        'propertyName' => 'cert_background',
]);

$this->belongsTo('CertificateLogo', [
       	'className' => 'Files',
       	'foreignKey' => 'certificate_logo_id',
        'propertyName' => 'cert_logo',
]);

but only the top association works.

if I do:

$this->belongsTo('CertificateBackgrounds',	['className'=>'Files'])->setForeignKey('certificate_back_id');
$this->belongsTo('CertificateLogo',	['className'=>'Files'])->setForeignKey('certificate_logo_id');

and create the entity files for “CertificateLogo” and “CertificateBackgrounds” which extend to “Files” entity then both associations work file.

I’m upgrading a project from cake 2 and this sort of association worked without defining propertyName or creating the entity file. is there a better way to do this. or am I doing this entirely wrong?

What does it mean “only the top works”?

Do you patch data into an entity and it doesn’t persist properly?
Do you contain the association in a query and it doesn’t fetch the data properly?

By default CakePHP uses the name of the association (so the first param) as the property name, so if you do

$this->belongsTo('CertificateBackgrounds',	['className'=>'Files'])->setForeignKey('certificate_back_id');
$this->belongsTo('CertificateLogo',	['className'=>'Files'])->setForeignKey('certificate_logo_id');

you should have ->certificate_backgrounds and ->certificate_logo on your entities.

thank you for responding so quickly :slight_smile:

$this->belongsTo('CertificateBackgrounds',	['className'=>'Files'])->setForeignKey('certificate_back_id');

when i use contain association CertificateBackgrounds in query cake throws error CertificateBackgrounds entity does not exist. have not tried patch. creating an entity file allows this to work:

<?php
namespace App\Model\Entity;
class CertificateBackground extends File {}

writing it this way without creating the entity file as above

$this->belongsTo('CertificateBackgrounds', [
       	'className' => 'Files',
       	'foreignKey' => 'certificate_back_id',
        'propertyName' => 'cert_background',
]);
# ... other associations 

that error goes away but only one of the association works. for example in my query i contain ['CertificateBackgrounds ',‘CertificateLogo’]. only the associated certificate background is returned in the query, no certificate logo

Cake doesn’t require you to have custom Entity classes for each custom Association. I have that myself, so somethings missconfigured in your app.

Do you have a custom Entity class set in your FilesTable class?

Yes we have lot of customizations extensions to the core classes just to allow us to bring old code in without too many changes. Plan is to wipe it away after critical functions are migrated over. I’ll look through the stack. thanks again :slight_smile:

Tried it in plain empty cake 5 app all works as expected. so not cakes fault.