2011-10-13 51 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

標記是正確的,單獨的關聯是不行的。 –