有一个问题,我们正在创建一个采用varchar的表单,但cakepp将该字段视为int。
那么我们该如何解决呢?
这是我们添加关系的添加函数。
public function add() {
$this->set('title_for_layout', 'Send A Relationship Request');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.jpg');
$this->layout='home_layout';
if ($this->request->is('post')) {
$this->Relationship->set($this->request->data);
if ($this->Relationship->validates(array('fieldList'=>array('receiver_id','Relationship.userExists'))))
{
$username=$this->Relationship->username;
$this->Relationship->save($this->request->data);
$this->Session->setFlash('The relationship has been saved');
} else {
$this->Session->setFlash('The relationship could not be saved. Please, try again.');
}
}
}
这是我们的观点(一个要求用户名(varchar)的表单)
<?php
echo $this->Form->create('Relationship', array('action'=>'add'));
echo $this->Form->input('sender_id',array('label'=>'Enter your username: '));
echo $this->Form->input('receiver_id',array('label'=>'Username of user: '));
echo "<br />";
echo $this->Form->end('Click here to add relationship');
?>
这是由于您的命名约定。Sender_id和receiver_id默认情况下将被视为整数值,因为它表示关系属于发送方,关系属于接收方。为了获得更好的结果,您可以更改字段的命名约定或使用以下代码:
echo $this->Form->input('sender_id',array('label'=>'Enter your username:',
'type'=>'text'));