RedBeanPHP多个FK到同一个表

时间:2013-05-27 15:03:52

标签: mysql foreign-keys redbean

我正在使用最新版本的RedBeanPHP。我正在塑造足球夹具结构。单个游戏由2个团队执行,属于单个游戏日(数据库中的固定)。

$o = R::dispense('game');
$o->l = R::load('team',$l[$i]);
$o->v = R::load('team',$v[$i]);
$o->fixture = R::load('fixture',$id);
$id = R::store($o);

在数据库中,RB创建2 Fk:

  • index_foreignkey_game_team
  • index_foreignkey_game_fixture

在插入游戏之后,这段代码不起作用:

$games = R::find('games',"fixture_id='$ID'");
foreach( $games as $o ):
    echo $o->l->id; // Cannot access to the Local Team
    echo $o->v->id; // Cannot access to the Visit Team
endforeach

谢谢!

1 个答案:

答案 0 :(得分:1)

只需使用别名告诉RedBeanPHP'v'和'l'是什么。

以下是:

//for testing only...
list($team1, $team2) = R::dispense('team', 2);

$game = R::dispense('game');
$game->team = R::load('team',$team1);
$game->visitor = R::load('team',$team2);
$id = R::store($game);
$theGame = R::load('game', $id);


echo 'team1: '.$theGame->team->id;
echo PHP_EOL;
//here I tell RedBeanPHP: visitor IS a team.
echo 'team2: '.$theGame->fetchAs('team')->visitor->id;

有关别名的更多详细信息:http://www.redbeanphp.com/aliasing

希望这有帮助!

干杯, 的Gabor