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

时间:2011-04-12 02:33:24

标签: cakephp

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

理事会控制器索引,问题是数字'region_id'显示在索引视图上而不是链接的'region->名称'。

    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 ;

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

将索引功能更新为:

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关系。