2011-10-13 83 views
0

我正在创建一个消息控制器,允许用户发送消息给其他用户。这里是我的数据库结构automagic下拉框不填充cakephp

用户

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> 

它显示一个下拉框,但它没有用户。我在用户表ATLEAST 5个用户

即使我添加像这样

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

仍然前缀不起作用

回答

0

像马克说,如果你不这样做这是行不通的遵守公约。重命名foreignkeys,发件人=> SENDER_ID,收件人=> 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'); 
+0

你的回答稍微不正确 - $ froms和$ tos会自动填充选择框。否则你需要一个手动'选项'=> $发件人等 – mark

+0

我现在不能测试它,但不是,它应该像这样工作,因为关联 – kaklon

+0

标记是正确的,单独的关联是不行的。 –