gpt4 book ai didi

mysql - RedBeanPHP 多个 FK 到同一个表

转载 作者:行者123 更新时间:2023-11-29 13:50:19 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com