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

为了更清楚(在下面的前两条评论之后改变)...... Councils Controller for index,问题是数字'region_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
--
-- Table structure for table `councils`
--

CREATE TABLE IF NOT EXISTS `councils` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) DEFAULT NULL,
`email` varchar(40) NOT NULL,
`website` varchar(40) NOT NULL,
`websource` varchar(40) NOT NULL,
`region_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;

-- --------------------------------------------------------

--
-- Table structure for table `regions`
--

CREATE TABLE IF NOT EXISTS `regions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
    
已邀请:
您正确地将区域列表传递给视图,但是您需要确保正确设置两个模型之间的关系。在您的委员会模型中,请确保您拥有以下内容:
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'));
}
您所要做的就是在视图中引用regions数组:
echo $regions[$councils['Council']['region_id']];
这将显示
Name
而不是
region_id
。 另外一件事,确保你也在Region Model中设置$ hasMany关系。     

要回复问题请先登录注册