doufan1899 2012-08-20 19:18 采纳率: 100%
浏览 28

yii“在view.php上尝试获取非对象的属性”

I have a strange problem with my yii application. I have two tables in my database Players - id, name, team_id and Teams - id, name. I can create new players but when I want to see player's profile there is an error -"Trying to get property of non-object" at this line:

'value'=>$model->team->NAME,

The most strange problem is that when I test url for players with Ids 1 and 2 everything is okay and I see the correct information, but for other ids I have this problem. Here is part of my code:

view.php

<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
    'ID',
    'NAME',
    'TEAM_ID', 
    array(
        'label'=>'Отбор',
        'type'=>'text',
        'value'=>$model->team->NAME,
    ),
),
)); ?>

Players.php

public function relations()
    return array(
      'team' => array(self::BELONGS_TO, 'TEAMS', 'ID'),
    );
}

Teams.php

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'player' => array(self::HAS_MANY, 'PLAYERS', 'ID'),
    );
}

PlayersController.php

public function actionView($id)
{
    $teams = new CActiveDataProvider('Teams');
    $players = new CActiveDataProvider('Players');
    $this->render('view', array(
        'model'=>$this->loadModel($id),
    ));
}
  • 写回答

1条回答 默认 最新

  • download92000 2012-08-20 19:50
    关注

    Seems like you need to fix bug with relations in Players model:

    public function relations()
        return array(
          'team' => array(self::BELONGS_TO, 'TEAMS', 'TEAM_ID'), // TEAM_ID instead of ID
        );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化