Welcome Guest, Not a member yet? Register   Sign In
Insert error when following the User Guide for Model
#1

[eluser]juddmuir[/eluser]
Hi,

I'm new to CI, and have been following the User Guide to get up to speed.

From the guide to models, the code to insert the model is this:

Code:
function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

When I run this code, I get the error
Code:
A Database Error Occurred

Error Number: 1054

Unknown column '_ci_ob_level' in 'field list'

It looks like the object $this contains variables other than just those in the database fields, although I haven't added any. What have I done wrong?
#2

[eluser]yelirekim[/eluser]
are you sure you're placing that code in a model and not in a controller?
#3

[eluser]juddmuir[/eluser]
Absolutely sure, tried and got the same with two different models.

Doing a var_dump($this) shows an ENORMOUS object has been instantiated, which I don't understand as all that's been done is the parent::Model() in the constructor.

For now, I'm creating a secondary object on the fly inside my model, to hold the data for inserting:


Code:
function save() {
    //Copy data into data model so CI doesn't error
    $datamodel->classified_title = $this->classified_title;
    $datamodel->classified_details = $this->classified_details;
    $this->db->insert('classifieds_post', $datamodel);
}

Horrible, but it works.
#4

[eluser]Michael Wales[/eluser]
Yes, it surprises me this hasn't been changed yet. $this refers to the CodeIgniter super-object (if you checkout libraries/model.php you will see the call to get_instance()).

The typical way of going about this is creating an array with the data to be inserted or creating a new object (like you have done). The object method you are using is kind of messy - since the object was never created/instantiated, I assume there isn't a valid definition for that object - it would throw errors in strict mode.
#5

[eluser]juddmuir[/eluser]
Thanks for confirming that I'm not going mad. I imagine a lot of users have hit the same error, and given up.

So, I guess the easiest way for now is to make an array rather than an object, i.e.

function save() {
$datamodel = array();
$datamodel['classified_title'] = $this->classified_title;
$datamodel['classified_details'] = $this->classified_details;
$this->db->insert('classifieds_post', $datamodel);
}
#6

[eluser]juddmuir[/eluser]
Thinking about it from a re-usable point of view, is there a clever bit of code to loop through the object's datamodel variables and look for matches in the current object?

That way you could declare an array when first instantiating the object, and when you do save(), or update() etc etc, you first call the function to populate the array, pulling out the object's data and stuffing it into the datamodel array. Obviously, you'd have to ensure that the datamodel array variables are the same name as the object's variables!

Hmm, I'll see if I can crack that one, not something I've tried before.




Theme © iAndrew 2016 - Forum software by © MyBB