cakephp / cakephp

CakePHP: The Rapid Development Framework for PHP - Official Repository
http://cakephp.org
MIT License
8.68k stars 3.44k forks source link

Not able to get protected `_junctionTableName` #17678

Closed mehov closed 1 month ago

mehov commented 1 month ago

Description

I have a belongsToMany association in TagsTable.php. In case it's important, it happens to be an association with itself. (Please don't ask why.)

$this->belongsToMany('Tags', [
    'foreignKey' => 'tag_id',
    'targetForeignKey' => 'tag_id',
    'joinTable' => 'tags_tags',
]);

Inside some method in the same TagsTable class I need to know the database name of the table they are associated through (tags_tags).

debug($this->getAssociation('Tags'));
object(Cake\ORM\Association\BelongsToMany) id:0 {
    protected _joinType => 'INNER'
    protected _strategy => 'select'
    protected _junctionTable => null
    protected _junctionTableName => 'tags_tags'
    protected _junctionAssociationName => null
    protected _junctionProperty => '_joinData'
    protected _saveStrategy => 'replace'
    protected _targetForeignKey => 'tag_id'
    protected _through => null
    protected _validStrategies => [ ]
    protected _dependent => true
    protected _targetConditions => null
    protected _junctionConditions => null
    protected _sort => null
    protected _name => 'Tags'
    protected _className => 'Tags'
    protected _bindingKey => null
    protected _foreignKey => 'tag_id'
    protected _conditions => [ ]
    protected _cascadeCallbacks => false
    protected _sourceTable => object(App\Model\Table\TagsTable) id:1 { }
    protected _targetTable => null
    protected _propertyName => null
    protected _finder => 'all'
    protected defaultTable => null
    protected _tableLocator => object(Cake\ORM\Locator\TableLocator) id:2 { }
}

So I need what's inside _junctionTableName, but I can't get it. There's no method to access that protected property listed on https://api.cakephp.org/4.4/class-Cake.ORM.Association.BelongsToMany.html and trying all combinations of $this->getAssociation('Tags')->junctionTableName() and $this->getAssociation('Tags')->getJunctionTableName() result in an error saying Unknown method.

CakePHP Version

4.4.14

PHP Version

No response

kojot1234 commented 1 month ago

There's a public function junction, that's probably what you are looking for. $this->getAssociation('Tags')->junction()

mehov commented 1 month ago

@kojot1234 thank you for replying.

I did try it, but I got another error

The Tags association on Tags cannot target the same table. InvalidArgumentException

Another problem with it is that over the last few years CakePHP has been moving to setSomething()/getSomething() and deprecating something(), so to me junction() felt like a candidate for deprecation that shouldn't be used anyway. So I didn't even mention it here

kojot1234 commented 1 month ago

Sounds to me like you need to provide a different alias for your association.

$this->belongsToMany('ChildTags', [
    'className' => 'Tags',
    'foreignKey' => 'tag_id',
    'targetForeignKey' => 'tag_id',
    'joinTable' => 'tags_tags',
]);
mehov commented 1 month ago

That worked. Thank you!