Cakephp键在索引列表中可见,但不是链接表的值

Cakephp键在索引列表中可见,但不是链接表的值,cakephp,Cakephp,为了更清楚(在下面的前两条评论后更改) 为索引指定控制器,问题是索引视图上显示的是数字“region\u id”,而不是链接的“region->name” function index() { $this->Council->recursive = 0; $this->set('councils', $this->paginate()); } 理事会模式: var $belongsTo = array( 'Region' =>

为了更清楚(在下面的前两条评论后更改)

为索引指定控制器,问题是索引视图上显示的是数字“region\u id”,而不是链接的“region->name”

    function index() {
    $this->Council->recursive = 0;
    $this->set('councils', $this->paginate());
}
理事会模式:

    var $belongsTo = array(
    'Region' => array(
        'className' => 'Region',
        'foreignKey' => 'region_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

var $hasMany = array(
    'Person' => array(
        'className' => 'Person',
        'foreignKey' => 'council_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);
SQL


您正在将区域列表正确地传递给视图,但需要确保两个模型之间的关系设置正确。在您的委员会模型中,确保您具有以下内容:

var $belongsTo = array(
    'Region' => array(
        'className' => 'Region',
        'foreignKey' => 'region_id'
    )
);

将索引函数更新为:

function index() {
    $this->Council->recursive = 0;
    $this->set('councils', $this->paginate());
    $regions = $this->Council->Region->find('list');
    $this->set(compact('regions'));
}
不,您只需在视图中引用区域数组:

echo $regions[$councils['Council']['region_id']];
这将显示
名称
,而不是
区域id


还有一件事,确保在区域模型中也设置了$hasMany关系。

这取决于
区域
委员会
的模型关联,以及
查找()
读取()
调用的
递归
选项。你应该发布它,而不是你的模式。Doh!谢谢你让我注意到这一点。我已经更新了我的问题,对不起,这不是很容易理解-希望现在是…;-)
echo $regions[$councils['Council']['region_id']];