Question

I'm using the latest version of RedBeanPHP. I'm modeling a football fixture structure. A single game, is performing by 2 teams and belong to a single game day (fixture in database).

$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);

In the database, RB creates 2 Fk:

  • index_foreignkey_game_team
  • index_foreignkey_game_fixture

And after inset the games, this code dosn't work:

$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

Thanks!

Was it helpful?

Solution

Simply use an alias to tell RedBeanPHP what 'v' and 'l' are.

Here is how:

//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;

For more details concerning aliasing: http://www.redbeanphp.com/aliasing

Hope this helps!

Cheers, Gabor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top