Wednesday, March 16, 2022

[FIXED] How to do association between a table with an ID and another table using the same ID but twice

Issue

In cakephp, I have a table users with primary key ID and another table friends with 2 foreign keys user_id, friend_id, both indexed to the same primary key in users table. I wanna know, how the heck do I connect them in the model?

Thanks!


Solution

You can use table alias.

    $this->belongsToMany('Friends', [
        'className' => 'Users',
        'foreignKey' => 'friend_id',
        'targetForeignKey' => 'user_id',
        'joinTable' => 'users_friends',
    ]);


Answered By - Kani

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.