Codeigniter数据库错误1364 MY_model

时间:2014-05-20 16:08:18

标签: php codeigniter mysql-error-1364

嗨我遇到codeiginiter问题我在codeigniter cms页面上创建新页面时遇到此错误。

  

发生数据库错误

     

错误号码:1364

     

字段'order'没有默认值

     

INSERT INTO pagestitleslugbodyparent_id)价值观   ('内容','内容','

     

网页内容

     

',0)文件名:/Applications/AMPPS/www/application/core/MY_Model.php

     

行号:62

我的MY_Model代码。

<?php
class MY_Model extends CI_Model {

protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;

function __construct() {
    parent::__construct();
}

public function array_from_post($fields){
    $data = array();
    foreach ($fields as $field) {
        $data[$field] = $this->input->post($field);
    }
    return $data;
}

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 timestamps
    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;
        $id = $filter($id);
        $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);

    if (!$id) {
        return FALSE;
    }
    $this->db->where($this->_primary_key, $id);
    $this->db->limit(1);
    $this->db->delete($this->_table_name);
}
 }

1 个答案:

答案 0 :(得分:1)

错误消息是不言自明的。您没有为order提供值,它是必需的,并且没有默认值。

你可以做一些事情......

  1. 更改架构以提供该字段的默认值
  2. 让它成为可空的
  3. 在查询中为其提供值