开发者

Select field in CakePHP retrieve data

I am trying开发者_如何学运维 to retrieve data from a simple table and populate a select field form. There are two tables, Acronyms and Topics.

Currently I have:

In my acronyms_controller (method add()):

$topics = $this->Acronym->Topic->find('list');
$this->set(compact('topics'));

In my acronym (model):

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

In my Topic model:

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

in my Acronym add view:

<?php echo $this->Form->input('topic_id'); ?>

I do not know why this is not working (it is not retrieving the Topic data in the select field). I download a simple application and did the same exercise and it did very well. I do not know what I am missing.

I would appreciate if I have your help.


Your code all appears to be correct. If the topic name is not appearing in the select field, then it is probably because the field in the database table is not correct.

For Cake to automagically popular the select with a list of Topic names, there must be a field in your Topictable called name.

Otherwise, there must be something else in your controller conflicting with this, but I'd need to see the entire add() method to determine this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜