Welcome Guest, Not a member yet? Register   Sign In
Error in documentation (Inserting into a database by setting object variables)
#1

[eluser]tmcw[/eluser]
I'm following the user guide on a new project, and <a href="
http://ellislab.com/codeigniter/user-gui...odels.html">this page</a> has the syntax

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

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

For inserting an entry. Using something very similar to that seems to try to insert all of the inherited properties of Model, and thus throws an error.

Code:
function insert_run()
    {
       // $this->date = $_POST['date'];
       // $this->tod = $_POST['tod'];
        $this->duration = $this->time_to_s($_POST['time_ms'], $_POST['time_s'], $_POST['time_m'], $_POST['time_h']);
    $this->distance = $_POST['distance'];
        $this->notes    = $_POST['notes'];
    //return $this->notes;
    $this->db->insert('runs', $this);
    }

Code:
An Error Was Encountered

Error Number: 1054

Unknown column '_parent_name' in 'field list'

INSERT INTO runs (id, date, tod, user_id, duration, distance, path_id, shoe_id, _parent_name, _ci_scaffolding, _ci_scaff_table, notes) VALUES ('', '', '', '', 2400, '5', '', '', 'Run_model', 0, 0, 'I ran today.')

Eh?
#2

[eluser]ELRafael[/eluser]
you don't have the _parent_name in the table.

try to use print_r or var_dump in the $this.

i use in another way.

Code:
//
$aData = array('id_field' => $id, 'name_field' => $name_value);
$this->db->insert('table_name', $aData);

in some cases (almost), $this has more than the keys/values to insert in the table!
#3

[eluser]Michael Wales[/eluser]
It's a known error within the documentation and it will actually save your hide a lot of the time. You don't want to just go inserting an object into a database without absolutely knowing, 100%, what is contained within that object.

Therefore, I always do something similar to this:

Code:
$insert->title = $this->input->post('title');
  $insert->content = $this->input->post('content');
  $insert->date = now();

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

This way - you know exactly what you are putting into your database. Takes a bit more typing, but security usually does.




Theme © iAndrew 2016 - Forum software by © MyBB