Fuelphp ORM无法访问相关表

时间:2013-10-25 12:48:30

标签: php orm fuelphp

我正在尝试使用ORM访问两个表(team,team_managers)。我收到错误“Fuel \ Core \ PhpErrorException [注意]:试图获取非对象的属性”。

这是抛出错误的代码:

<?php
$team = Model_Team::find('all', array('related' => array('team_managers')));
$team_managers = $team->team_managers;

foreach($team_managers as $tm){
    echo $tm->id;
}

这样做有效,但我认为它违背了ORM的目的:

<?php
$team = Model_Team::find('all', array('related' => array('team_managers')));
$team_managers = Model_Team_Manager::find('all');

foreach($team_managers as $tm){
    echo $tm->id;
}

在团队模型中,我有这种关系:

protected static $_has_many = array('team_managers' => array(
'model_to' => 'Model_Team_Manager',
'key_from' => 'id',
'key_to' => 'team_id',
));

如何让它与$ team-&gt; team_managers一起使用?

1 个答案:

答案 0 :(得分:0)

我认为你在做什么是好的,我不明白为什么你试图访问$team->team_managers

{p> $teamModel_Team个对象的数组中,这是您的错误。

尝试以下方法:

$teams = Model_Team::find('all', array('related' => array('team_managers')));

foreach ($teams as $team) {
    foreach ($team->team_managers as $team_manager) {
        echo $team_manager->id;
    }
}