0

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

用户

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');

还是不行

4

1 回答 1

0

Like stated by Mark it doesn't work if you don't stick to convention. Rename the foreignkeys, Sender => sender_id, Recipient => recipient_id

You have to make the lists in the controller first

in the message controller

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

Then in the view

echo $this->Form->input('recipient_id');
echo $this->Form->input('sender_id');
于 2011-10-13T14:35:44.473 回答