2013-05-27 56 views
0

我正在使用最新版本的RedBeanPHP。我正在模拟一个足球夹具结构。单场比赛,由2支球队执行,属于单一比赛日(数据库中的夹具)。RedBeanPHP多个FK到同一张表

$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 

谢谢!

+0

我认为这是东阳领域还没有得到相同的名称为FK表。例如,如果我将球队重新命名为“l”,那么球队的情况会很好,但我需要有2支球队,而且我不能有两个球队名叫“球队”。 – mauriblint

回答

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