【问题标题】:Cakephp: How to add variable value to tableCakephp:如何将变量值添加到表中
【发布时间】:2014-07-21 13:16:02
【问题描述】:

如何使用 cakephp formhelper 将变量值添加到表中。在我的表单中,我允许用户输入一些值,但用户的 IP 地址也添加到表中。在插入数据库之前,我还会计算用户的输入。

我应该修改 cakephp 中的控制器或模型吗?我有一个编号表,其中包含 id、cybernumber、created、department、ipaddress。非常感谢。

//Controller/NumberController.php
public function add() 
{
    if ($this->request->is('post')) 
    {
        $this->Number->create();
        //not working 
        //$this->request->data['Number']['ipaddress'] = $this->request->clientIp();
        //not working 
        //$this->Number->set('ipaddress',$this->request->clientIp());

        if ($this->Number->save($this->request->data)) 
        {
            $this->Session->setFlash(__('OK.'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unable to add your post.'));
        }
    }
}

// Model/Number.php
<?php

class Number extends AppModel {

    public $validate = array(
        'cybernumber' => array(
            'rule' => 'isUnique',
            'message' => 'unique only'
        ),
        'department' => array(
            'rule' => 'notEmpty'
        )

    );
}

【问题讨论】:

    标签: php cakephp form-helpers


    【解决方案1】:

    首先在 Config/core.php 中修改:Configure::write('debug', 0);配置::write('debug', 1);

    之后

    $this->Number->create();
    $this->request->data['Number']['ipaddress'] = $this->request->clientIp();
    

    添加

    debug($this->request->data);
    

    检查ipaddress 字段是否存在。

    尝试检查数据库中的字段类型。可能是数据格式不兼容吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 2012-02-20
      • 2020-10-15
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多