1

有一个问题,我们正在创建一个采用 varchar 的表单,但 cakephp 将该字段视为一个 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');

?>
4

1 回答 1

2

这是由于您的命名约定。默认情况下,Sender_id 和 receiver_id 将被视为整数值,因为它表示关系属于发送者和关系属于接收者,为了获得更好的结果,您可以更改字段的命名约定或使用以下代码:

echo $this->Form->input('sender_id',array('label'=>'Enter your username:',
                                           'type'=>'text'));
于 2012-07-26T05:48:38.240 回答