Я использую RedBeanPHP. Предположим, у меня есть следующая структура:
<?php
require_once 'redbean/RedBean/redbean.inc.php';
R::setup('sqlite:test.sqlite');
$user = R::dispense('user', 1);
$user->name = 'john';
R::store($user);
$post = R::dispense('post', 1);
$post->author = $user;
$post->text = 'great post';
R::store($post);
$comment = R::dispense('comment', 1);
$comment->post = $post;
$comment->author = $user;
$comment->text = 'great comment';
R::store($comment);
Теперь я хочу получить всех авторов комментариев, не загружая их вручную (т.е. я хотел бы избежать кода, подобного R::find('comment', 'post_id = ?', [1])
). Я делаю это так:
$post = R::load('post', 1);
R::preload($post,
[
'author' => 'user', //this makes $post->author->name work
'comment',
//what I've tried in order to get $post->comments[...]->author->name to work:
//'comment.author',
//'comment.author' => 'comment.user',
//'comment.author' => 'user',
//'author' => 'comment.user',
]);
echo 'post author: ' . $post->author->name
. PHP_EOL;
foreach ($post->ownComment as $comment)
{
echo 'comment: ' . $comment->text
. ' by ' . $comment->author->name
. PHP_EOL;
}
Моя проблема в том, что он печатает что-то вроде этого:
post author: john
comment: great comment by
Как видите, информации об авторе комментария нет. Что я могу с этим сделать, кроме как выбирать комментарии/авторов вручную?