Part of the EllisLab Network
   
 
Active record insert error
Posted: 01 November 2006 02:29 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  6
Joined  10-31-2006

Hi again, I’m coding a model for a user database and I’m using active record for insert.

In the model documentation you can find this code:

class Blogmodel extends Model {

    
var $title   = '';
    var
$content = '';
    var
$date    = '';

    function
insert_entry()
    
{
        $this
->title   = $_POST['title'];
        
$this->content = $_POST['content'];
        
$this->date    = time();

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

It uses the object variables ($this) for the insert, but when I try in my own model I get this error:

An Error Was Encountered

Error Number
: 1054

Unknown column
'_parent_name' in 'field list'

INSERT INTO users (login, user_name, since, password, question, answer, user_status, remind, _parent_name, _ci_scaffolding, _ci_scaff_table) VALUES ('john@doe.com', 'johndoe', '2006-11-01 20:22:46', '621f11f70193417445dfe0581ed16ee270a6dff9', 'test word', '717bdea6ed86abaef6f4257a948b1b98e882a474', 'A', 'I', 'Users_model', 0, 0)

I see that there are 3 internal variables of Model in the insert (_parent_name, _ci_scaffolding, _ci_scaff_table), so either the doc is wrong or there is a bug.


Thank you.

 Signature 

The cat ate my source code.

Profile
 
 
Posted: 01 November 2006 05:16 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  144
Joined  09-08-2006

i see someone hasnt read the manual smile

active record insert

Profile
 
 
Posted: 02 November 2006 02:14 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  6
Joined  10-31-2006

Hi, I’ve read the manual, in fact, I have use the section that you link to solve the problem smile. But in my first try I used the example in the Model section, and it doesn’t work ...

 Signature 

The cat ate my source code.

Profile
 
 
Posted: 27 March 2007 08:22 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  03-02-2007

I find this a bit weird, why do we set a model class ? I first set it to be able to do then :

function save_data {
$this
->db->update('event', $this);
}

But we can’t we must do :

function save_data {
$this
->db->set('name', $this->name);
$this->db->set('surname', $this->surname);
etc...
$this->db->update('event', $this);
}

This is not the smartest way i think ...

Profile
 
 
Posted: 27 March 2007 10:58 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

Correct code:

class Blogmodel extends Model {

    
var $data = array();

    function
insert_entry()
    
{
        $this
->data['title']   = $_POST['title'];
        
$this->data['content'] = $_POST['content'];
        
$this->data['date']    = time();

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

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 06 July 2007 10:07 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  4
Joined  07-05-2007

Hello,

I’m just getting started with CI and ran into this issue. It seems to me that not being able to do:

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

Is a major issue (plus it means the user_guide is incorrect). I may not be understanding it correctly, but if we already have properties of the object, why do we need to create another array to hold those properties, just to do a DB transaction?

Is it because it is assumed that all data will come directly from a $_POST? I’m just curious what the reasoning was for “removing” that capability….

Profile
 
 
Posted: 06 July 2007 11:43 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  747
Joined  02-05-2007
emartin24 - 06 July 2007 10:07 AM

Hello,

I’m just getting started with CI and ran into this issue. It seems to me that not being able to do:

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

Is a major issue (plus it means the user_guide is incorrect). I may not be understanding it correctly, but if we already have properties of the object, why do we need to create another array to hold those properties, just to do a DB transaction?

Is it because it is assumed that all data will come directly from a $_POST? I’m just curious what the reasoning was for “removing” that capability….

I’m not really answering your question, but by using the php function “get_object_vars” I think you could do this:

// array get_object_vars ( object $object )
$this->db->insert('entries', get_object_vars($this));

 Signature 

“I am the terror that flaps in the night”

Profile
 
 
Posted: 11 July 2007 10:22 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  4
Joined  07-05-2007

Thanks for the suggestion. I haven’t tested it, but I believe that the 3 extra vars would still be there. I’m going to add a method that removes those 3 vars for inserts and updates. I’ll post my fix here, in case any one else is interested.

Profile
 
 
Posted: 22 November 2007 06:39 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  03-02-2007

Is it planned to modify the DB_active_rec.php for the next release to allow update code like in the user_guide ($this->db->insert(‘table’,$this) ?

Profile
 
 
Posted: 22 November 2007 07:29 AM   [ Ignore ]   [ # 9 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6712
Joined  03-23-2006

There are no current plans to that effect wouaren that I’m aware of.  For now you’ll need to either hack the code, or just use it as documented.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 22 November 2007 08:33 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  03-02-2007

Ok thank you

Should the documentation be updated? I’m talking about : http://codeigniter.com/user_guide/general/models.html

Profile
 
 
Posted: 22 November 2007 08:52 AM   [ Ignore ]   [ # 11 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6712
Joined  03-23-2006

Oh, D’uh.  Sorry, didn’t realize the docs were incorrect.  Yes, I’ll look into that.  Thanks.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 22 November 2007 01:42 PM   [ Ignore ]   [ # 12 ]  
Moderator
Avatar
RankRankRankRank
Total Posts:  1827
Joined  07-30-2007

It’s nice we get the documentation updated, but guys - please don’t just INSERT that object, you are opening your application up to get really destroyed.

Let’s take the following table for instance:

id INT NOT NULL AUTO_INCREMENT
email VARCHAR
(40)
password VARCHAR(100)
role ENUM('user', 'admin', 'root'), DEFAULT 'user'

Now, we have our registration form, and it has a field for email and password. When someone fills out that form we make sure their email is a valid_email, their email address hasn’t been registered before, hell - maybe we even check their password strength. And then we perform:

$this->db->insert('users', $this);

Should be good right? We’re inserting an email and password, and using the AUTO_INCREMENT and DEFAULTs for id and role.

What if I add the following field to your form, and submit that form:

<input type="text" name="role" value="root" />

I’Z ON YUR SERVRZ, STEELING ALL YR ROOTZ! At least as far as your application is concerned.

Always always always validate your data. Who it came from (IP logging etc), what the data is, when it came, where it came from (make sure the form processor is validating it’s referrer), and why it’s coming.

 Signature 

MichaelWales.com

Profile
 
 
Posted: 22 November 2007 02:12 PM   [ Ignore ]   [ # 13 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  03-02-2007

That’s why you must double check your application validations !

Profile
 
 
   
 
 
‹‹ Scaffolding issue      uri with 1.5.0 ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64458 Total Logged-in Users: 26
Total Topics: 80971 Total Anonymous Users: 1
Total Replies: 435706 Total Guests: 197
Total Posts: 516677    
Members ( View Memberlist )