New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested hasOne relationship with alias corresponding to database field #6372
Comments
What is the stack trace on that notice error? Is there a reason you aren't using the foreignKey config option to customize the column used instead of defining the conditions? |
I'm using a generic Medias table that is used by others Model so I try not to touch it and keep it generic, that's why I keep the foreign key on the "calling" table. Stack trace : Cake\Database\Type\IntegerType::toPHP() - CORE/src/Database/Type/IntegerType.php, line 62
Cake\ORM\ResultSet::_castValues() - CORE/src/ORM/ResultSet.php, line 507
Cake\ORM\ResultSet::_groupResult() - CORE/src/ORM/ResultSet.php, line 440
Cake\ORM\ResultSet::_fetchResult() - CORE/src/ORM/ResultSet.php, line 345
Cake\ORM\ResultSet::valid() - CORE/src/ORM/ResultSet.php, line 232
Cake\ORM\Association\HasMany::_buildResultMap() - CORE/src/ORM/Association/ExternalAssociationTrait.php, line 114
Cake\ORM\Association\HasMany::eagerLoader() - CORE/src/ORM/Association/SelectableAssociationTrait.php, line 56
Cake\ORM\EagerLoader::loadExternal() - CORE/src/ORM/EagerLoader.php, line 538
Cake\ORM\Query::_execute() - CORE/src/ORM/Query.php, line 644
Cake\ORM\Query::_all() - CORE/src/Datasource/QueryTrait.php, line 218
Cake\ORM\Query::all() - CORE/src/ORM/Query.php, line 595
Cake\ORM\Query::first() - CORE/src/Datasource/QueryTrait.php, line 340
Cake\ORM\Query::firstOrFail() - CORE/src/Datasource/QueryTrait.php, line 351
Cake\ORM\Table::get() - CORE/src/ORM/Table.php, line 1108
App\Controller\ArtistsController::collection() - APP/Controller/ArtistsController.php, line 54
Cake\Controller\Controller::invokeAction() - CORE/src/Controller/Controller.php, line 405 And the $this->belongsTo('DefaultImage', [
'className' => 'Media.Medias',
'foreignKey' => 'default_image',
// 'conditions' => [$this->alias().'.default_image = DefaultImage.id'],
// 'propertyName' => 'default_photo'
]); Gives the same result, and same value in field (1). Only working way is to give a different property name. |
So having an association property shadow/share a name with the foreign key field is not going to work. While it conceptually seems cleaner, the ORM will have no way to stitch data together, or decompose the various entity trees into the necessary SQL statements. You're going to have to choose different names for the foreign key columns and entity property names. |
Ok, though it was an issue. Using different name since now, thanks. |
I have a
Products
table, which has a fielddefault_sku
corresponding to an entry in the tableSkus
, which has a fielddefault_image
corresponding to an entry in the tableMedias
.With defined associations :
And
I retrieve the associations like this:
Cakephp tries to convert the default_image association to an int :
It only does this when the DefaultImage association property name is the same as the database field (default_image). If I change the property to DefaultImaged for example, works good.
Notice that it doesn't do it for the DefaultSku association even if the database field is the same name than the association (default_sku), this time it gets correctly replace by the Sku entity.
The text was updated successfully, but these errors were encountered: