automagic下拉框未填充cakephp

automagic下拉框未填充cakephp,php,mysql,cakephp,cakephp-model,Php,Mysql,Cakephp,Cakephp Model,我正在创建一个允许用户向其他用户发送消息的消息控制器。这是我的数据库结构 使用者 信息 to_id和from_id都引用用户中的id列。这是我的消息模型 消息 仍然不起作用就像马克所说的,若你们不遵守惯例,它就不起作用。重命名foreignkey,发送方=>Sender\u id,接收方=>Recipient\u id 您必须首先在控制器中创建列表 在消息控制器中 $senders = $this->Message->Sender->find('list'); $r

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

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

消息
仍然不起作用

就像马克所说的,若你们不遵守惯例,它就不起作用。重命名foreignkey,发送方=>Sender\u id,接收方=>Recipient\u 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');

您的答案有点不正确-$FROM和$tos将自动填充选择框。否则,您需要手动“选项”=>$senders etcI现在无法测试它,但不行,它应该是这样工作的,因为关联性我同意,应该将ForeignKey更改为sender_id和recipient_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>
    echo $this->Form->input('Sender.to_id');
    echo $this->Form->input('Recipient.from_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');