0

I have simple model, that needs to inset data to my table. All works fine when i use sql and query but when i try to use insert function i get an error that i need to use set ??

id in my database is auto increment and date is time stamp

class Article extends CI_Model {

private $id;
private $title;
private $text;
private $date;

public function __construct()
{
    $this->load->database();
}

public function save_article($title,$text)
{
    $this->title=$title;
    $this->text=$text;
    $this->id='';
    $this->load->helper('date');
    $this->date=now();
    print_r($this);
    //"INSERT INTO article VALUES ('','".$title."','".$text."',NOW())")
    $this->db->insert('articles',$this);
}
}

Article Object ( [id:Article:private] => [title:Article:private] => rrrrrrrrrrr [text:Article:private] => rrrrrrrrrrrrrrrrrr [date:Article:private] => 1351680951 )

A Database Error Occurred

You must use the "set" method to update an entry.

Filename: C:\Program Files (x86)\EasyPHP-12.1\www\blog\system\database\DB_active_rec.php

Line Number: 1174

4
  • 1
    It might be that your id database field is unique, and there is already an id with the value ''. Commented Oct 31, 2012 at 11:16
  • my id in db is unique and AUTO_INCREMENT ! it does not need to insert that value , i tried without setting id but again i was getting same error Commented Oct 31, 2012 at 11:19
  • where is db defined? You use this but it is not in the constructor...try setting this->id = false Commented Oct 31, 2012 at 11:19
  • you must set the data using set() before insert the data into database Commented Mar 11, 2013 at 20:55

1 Answer 1

5

Your member variables in Article are private. The Active Record class cannot read them using get_object_vars.

private $id;
private $title;
private $text;
private $date;

Make them as public or pass in an array of key-value pairs to insert.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.