1

Hi i am having a problem with codeiginiter i am getting this error when i create a new page on my codeigniter cms page.

A Database Error Occurred

Error Number: 1364

Field 'order' doesn't have a default value

INSERT INTO pages (title, slug, body, parent_id) VALUES ('Content', 'content', '

Page content

', 0) Filename: /Applications/AMPPS/www/application/core/MY_Model.php

Line Number: 62

My code for 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);
}
 }
user3596309
  • 33
  • 1
  • 1
  • 9

1 Answers1

1

The error message is self explanatory. You're not supplying a value for order, it is required, and there is no default value.

You can do a few things..

  1. Change your schema to provide a default for that field
  2. Make it nullable
  3. Provide a value for it in your query
mituw16
  • 5,126
  • 3
  • 23
  • 48
  • Can you mark where i would change this. I am still learning php and the tutorial i am following doesn't get or explain this error. – user3596309 May 20 '14 at 16:14
  • The first two items I suggested have nothing to do with php, those are database changes. The third item you would simply include a value for order like `INSERT INTO pages (title, slug, body, parent_id, order) VALUES ('Content', 'content', 'body', some_id, some_order)` – mituw16 May 20 '14 at 16:15
  • this is what is currently in the sql for pages in the database INSERT INTO `pages`(`id`, `title`, `slug`, `order`, `body`, `parent_id`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6]) – user3596309 May 20 '14 at 16:18
  • Adding Null to order in the tables worked. Will this cause any issues? – user3596309 May 20 '14 at 16:31
  • @user3596309 Hard to say without knowing the specs of your application. It might, but I can't answer that. – mituw16 May 20 '14 at 16:32