【问题标题】:RedBeanPHP multiple FK to the same tableRedBeanPHP 多个 FK 到同一个表
【发布时间】:2013-05-27 15:03:51
【问题描述】:

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

谢谢!

【问题讨论】:

  • 我认为是因为该字段与 fk 表的名称不同。例如,如果我将字段“l”重命名为“team”,它工作正常,但我需要有 2 个团队,而且我不能很自然地有两个名为“team”的字段。

标签: mysql foreign-keys redbean


【解决方案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

希望这会有所帮助!

干杯, 嘉柏

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多