[英]automagic drop down box is not populating cakephp
I am creating a message controller that allows the user send messages to other users. 我正在创建一个消息控制器,允许用户将消息发送给其他用户。 Here is my database structure
这是我的数据库结构
id
username
password
id
to_id
from_id
subject
message
the to_id and from_id both reference the id column in users. to_id和from_id都引用用户中的id列。 Here is my model for messages
这是我的消息模型
<?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'
)
);
}
Then here is my view 那是我的看法
<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>
It displays a drop down box, but no users in it. 它显示一个下拉框,但其中没有用户。 I have atleast 5 users in the users table
我的用户表中至少有5位用户
even if i add prefix like so 即使我像这样添加前缀
echo $this->Form->input('Sender.to_id');
echo $this->Form->input('Recipient.from_id');
still doesn't work 仍然不起作用
Like stated by Mark it doesn't work if you don't stick to convention. 就像Mark所说的那样,如果不遵守约定就行不通。 Rename the foreignkeys, Sender => sender_id, Recipient => recipient_id
重命名外键,发件人=> sender_id,收件人=> receive_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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.