Skip to content
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

Closed
yachaka opened this issue Apr 20, 2015 · 4 comments
Closed

Nested hasOne relationship with alias corresponding to database field #6372

yachaka opened this issue Apr 20, 2015 · 4 comments

Comments

@yachaka
Copy link

yachaka commented Apr 20, 2015

I have a Products table, which has a field default_sku corresponding to an entry in the table Skus, which has a field default_image corresponding to an entry in the table Medias.
With defined associations :

        $this->hasOne('DefaultSku', [
            'className' => 'Skus',
            'foreignKey' => false,
            'conditions' => [$this->alias().'.default_sku = DefaultSku.id']
        ]);

And

        $this->hasOne('DefaultImage', [
            'className' => 'Media.Medias',
            'foreignKey' => false,
            'conditions' => [$this->alias().'.default_image = DefaultImage.id']
        ]);

I retrieve the associations like this:

        $artist = $this->Artists->get($id, [
            'contain' => ['Photo', 'Products.DefaultSku.DefaultImage']
        ]);

Cakephp tries to convert the default_image association to an int :

    Notice (8): Object of class Media\Model\Entity\Media could not be converted to int [CORE/src/Database/Type/IntegerType.php, line 62]

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.

@markstory markstory added this to the 3.0.3 milestone Apr 20, 2015
@markstory
Copy link
Member

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?

@yachaka
Copy link
Author

yachaka commented Apr 20, 2015

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 default_image field contains the value 1.
EDIT: I can use the belongsTo association, just doesn't feel right. Don't know what you think about this.
EDIT2:

        $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.

@markstory
Copy link
Member

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.

@yachaka
Copy link
Author

yachaka commented Apr 20, 2015

Ok, though it was an issue. Using different name since now, thanks.

@yachaka yachaka closed this as completed Apr 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants