我正在使用最新版本的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


谢谢!

最佳答案

只需使用别名即可告诉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

希望这可以帮助!

干杯,
加博尔

关于mysql - RedBeanPHP将多个FK移到同一表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16775903/

10-10 03:33