Closed mehov closed 1 month ago
There's a public function junction, that's probably what you are looking for.
$this->getAssociation('Tags')->junction()
@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
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',
]);
That worked. Thank you!
Description
I have a
belongsToMany
association inTagsTable.php
. In case it's important, it happens to be an association with itself. (Please don't ask why.)Inside some method in the same
TagsTable
class I need to know the database name of the table they are associated through (tags_tags
).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