Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A How to use self referential N-M relationship to make 2 users friends using RedBean PHP ORM?

I am working with PHP and RedBean PHP ORM. I have a table user like so: id | username ---|--------- 1 | Alice 2 | Bob I want Alice and Bob to become friends. So I want to create M-M juncti...

0 answers  ·  posted 1y ago by Vanity Slug ❤️‭

#1: Initial revision by user avatar Vanity Slug ❤️‭ · 2023-06-19T12:55:56Z (over 1 year ago)
How to use self referential N-M relationship to make 2 users friends using RedBean PHP ORM?
I am working with PHP and [RedBean PHP ORM](https://redbeanphp.com/index.php). I have a table `user` like so:

```
id | username
---|---------
 1 | Alice
 2 | Bob
```

I want Alice and Bob to become friends. So I want to create M-M junction table `friend` that will look something like this:

```
id | friend_1_id | friend_2_id
---|-------------|------------
 1 |           1 |           2
```

`friend_1_id` and `friend_2_id` should be a foreign key reference to `user`.

In RedBean PHP ORM documentation at the bottom of [Many-to-Many](https://redbeanphp.com/index.php?p=/many_to_many) page there is a section titled "[Self referential N-M](https://redbeanphp.com/index.php?p=/many_to_many#self_referential_many_to_many)". Here is the content of that section:

 > You can have a shared list containing beans of the same type as the owner of the list: 
\
\
`$friends = $friend->sharedFriend;`
\
\
In this case RedBeanPHP will operate in a special self-referential many-to-many relationship mode. It will not only retrieve all friends of $friend, but also all other friends that are associated with $friend.

I don't find this section to be informative enough.
I suspect that this is exactly what I need, however, I cannot figure out how to make it work. Here is what I tried:

```
$u1 = R::findOne("user", "id = 1");
$u2 = R::findOne("user", "id = 2");
$u1->sharedFriend = $u2;
R::store($u1);
```

Instead of creating `friend` table and linking 2 users it added `sharedFriend_id` column to `user` table and now it looks like this:

```
id | username | sharedFriend_id
---|----------|----------------
 1 | Alice    | 2
 2 | Bob      | NULL
```

Questions:

- How can I implement self-referential N-M with RedBean PHP?
- If "self-referential N-M" is not the correct approach for the problem I want to solve then what would be the most "RedBean PHP"-ish way of making two users friends?

---

<sub>
Note: In the past I managed stuff like this manually by writing all the necessary logic myself. I would rather avoid doing that and learn how to do it properly using this framework if it is possible.
</sub>