Php Yii框架数据库关系

Php Yii框架数据库关系,php,activerecord,yii,Php,Activerecord,Yii,我在定义类关系时遇到问题 在此之前,让我向您展示我的数据库结构 Agent table id username password Views table id agent_id accessor_id 一个代理可以允许许多代理查看他们的帖子。表视图保存代理所有者和允许查看其发布的代理的数据 我对视图模型的关系声明如下: /** * @return array relational rules. */ public funct

我在定义类关系时遇到问题

在此之前,让我向您展示我的数据库结构

    Agent table
    id
    username
    password

    Views table
    id
    agent_id
    accessor_id
一个代理可以允许许多代理查看他们的帖子。表视图保存代理所有者和允许查看其发布的代理的数据

我对视图模型的关系声明如下:

/**
 * @return array relational rules.
 */
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(
        'agents' => array(self::BELONGS_TO, 'Agent', 'agent_id'),
    );
}
/**
 * @return array relational rules.
 */
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(
        'groups'=>array(self::BELONGS_TO, 'Group', 'group_id'),
        'views' => array(self::BELONGS_TO, 'View', 'agent_id'),
    );
}
我对代理模型的关系声明如下:

/**
 * @return array relational rules.
 */
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(
        'agents' => array(self::BELONGS_TO, 'Agent', 'agent_id'),
    );
}
/**
 * @return array relational rules.
 */
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(
        'groups'=>array(self::BELONGS_TO, 'Group', 'group_id'),
        'views' => array(self::BELONGS_TO, 'View', 'agent_id'),
    );
}
我在尝试运行应用程序时收到以下错误

The relation "views" in active record class "Agent" is specified with an invalid foreign key "agent_id". There is no such column in the table "agents".
我怎样才能解决这个问题?请帮忙。谢谢大家!

代理->视图不属于关系。它要么有一个,要么有多个:


您应该只需要在视图模型中定义关系

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(
        'agents' => array(self::BELONGS_TO, 'Agent', 'agent_id'),
    );
}
代理模型中不需要定义关系

在控制器端,您可以找到这样的记录

$model = View::model()->with('agents')->findAll();
在这个$model中,您拥有所有视图记录和代理


希望这对您有用..

但我无法显示以下数据:无法是什么意思?若它是空的,那个么可能只是视图表中并没有相关数据。