自动下拉框不会填充cakephp

时间:2011-10-13 13:38:03

标签: php mysql cakephp cakephp-appmodel

我正在创建一个消息控制器,允许用户向其他用户发送消息。这是我的数据库结构

的用户

id
username
password

的消息

id
to_id
from_id
subject
message

to_id和from_id都引用用户的id列。这是我的消息模型

消息

<?php
class Message extends AppModel {
    var $name = 'Message';
    var $displayField = 'subject';


    var $validate = array(
        'id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'to_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                'allowEmpty' => false,
                'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'from_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                'allowEmpty' => false,
                'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
            );

        var $belongsTo = array(
                'Sender' => array(
                        'className' => 'User',
                        'foreignKey' => 'to_id'
                        ),
                'Recipient' => array(
                        'className' => 'User',
                        'foreignKey' => 'from_id'
                        )
                );
}

然后这是我的观点

<div class="messages form">
<?php echo $this->Form->create('Message');?>
    <fieldset>
        <legend><?php __('Add Message'); ?></legend>
    <?php
        echo $this->Form->input('to_id');
        echo $this->Form->input('from_id');
        echo $this->Form->input('subject');
        echo $this->Form->input('message');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
    <h3><?php __('Actions'); ?></h3>
    <ul>

        <li><?php echo $this->Html->link(__('List Messages', true), array('action' => 'index'));?></li>
    </ul>
</div>

它会显示一个下拉框,但不会显示任何用户。我在用户表中至少有5个用户

即使我像这样添加前缀

    echo $this->Form->input('Sender.to_id');
    echo $this->Form->input('Recipient.from_id');

仍然无效

1 个答案:

答案 0 :(得分:0)

如同Mark所说的那样,如果你不遵守惯例就行不通。重命名外键,Sender =&gt; sender_id,收件人=&gt; recipient_id

您必须先在控制器中制作列表

消息控制器中的

   $senders = $this->Message->Sender->find('list');
   $recipients = $this->Message->Recipient->find('list');
   $this->set(compact('senders', 'recipients'));

然后在视图中

echo $this->Form->input('recipient_id');
echo $this->Form->input('sender_id');