调用未定义的函数inval()

时间:2014-03-24 07:46:29

标签: php codeigniter codeigniter-2

我在CodeIgniter中的My_Model中有错误。我到处都看,但没有My_Model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class MY_Model extends CI_Model
    {
    protected $_table_name = '';
    protected $_primary_key = 'id';
    protected $_primary_filter = 'intval';
    protected $_order_by = '';
    protected $_rules = array();
    protected $_timestamps = FALSE;
    function __construct(){
        parent::__construct();
        $this->load->database();
    }
    public function get($id = NULL, $single = FALSE)
    {
        if ($id != NULL)
        {   
            $filter = $this->_primary_filter;
            $id = $filter($id);
            $this->db->where($this->_primary_key,$id);
            $method = 'row';
        }
        elseif($single == TRUE)
        {
            $method = 'row';
        }
        else
        {
            $method = 'result';
        }   
        if(!count($this->db->ar_orderby))
        {
            $this->db->order_by($this->_order_by);  
        }
        return $this->db->get($this->_table_name)->$method();
    }
    public function get_by($where, $single = FALSE)
    {
        $this->db->where($where);
        return $this->get(NULL, $single);
    }
    public function save($data, $id = NULL)
    {
        //Set timestamp
        if($this->_timestamps == TRUE)
        {
            $now = date('Y-m-d H:i:s');
            $id || $data['created'] = $now;
            $data['modified'] = $now;
        }
        //INSERT
        if ($id ===NULL)
        {
            !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
            $this->db->set($data);
            $this->db->insert($this->_table_name);
            $id = $this->db->insert_id();
        }
        //UPDATE
        else
        {
            $filter = $this->_primary_filter;
            $this->db->set($data);
            $this->db->where($this->_primary_key,$id);
            $this->db->update($this->_table_name);
        }
        return $id;
    }
    public function delete($id)
    {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key,$id);
        $this->db->limit(1);
        $this->db->delete($this->_table_name);
    }

}
?>

在我的控制器中,我以这种方式调用函数:

public function delete()
    {
        $this->page_m->delete(3);
    }

当我写:cms / page / delete ...错误是:

Fatal error: Call to undefined function inval() in C:\xampp\htdocs\application\core\MY_Model.php on line 75

请帮帮我

2 个答案:

答案 0 :(得分:0)

我认为您的问题出在 delete() 函数

$filter = $this->_primary_filter;
$id = $filter($id);

确保您收到正确的 id type (int,bool)

答案 1 :(得分:0)

改变你的:

$filter = $this->_primary_filter;
$id = $filter($id);

致:

$id = intval($id);