对此的解决方案实际上与发布的内容有点倒退。您需要创建一条消息并为其分配两个用户模型,而不是相反。这是一个例子:
// Make some models
list($sender, $recipient) = R::dispense('user', 2);
$message = R::dispense('message');
// Set some data
$sender->name = 'Sender';
$recipient->name = 'Recipient';
$message->title = "Hello world";
// Associate users and message
$message->sender = $sender;
$message->recipient = $recipient;
// Store everything
R::store($message);
真的很容易。RedBean 负责在R::store($message)
调用时存储更新的用户信息。消息表如下所示:
+----+-------------+-----------+--------------+
| id | title | sender_id | recipient_id |
+----+-------------+-----------+--------------+
| 6 | Hello world | 3 | 4 |
+----+-------------+-----------+--------------+