Welcome Guest, Not a member yet? Register   Sign In
Model help noob
#1

[eluser]dmisterct[/eluser]
I just started developing with models and I need help with the insert portion, I have a Model something along
Code:
<?php
class Mymodel extends CI_Model {
  var field1;
  var field2;
  var field3;
  
  function __construct() {
     parent::__construct();
  }

  function insert_entry($field1, $field3) {
    $this->field1 = $field1;
    $this->field2 = $this->input->post('field2');
    $this->field3 = $field3;
    $this->db->insert('table', $this);   // This line does not work, it does not execute this statement
  }
}
?>

and a Controller like:

Code:
<?php
class MyController extends CI_Controller {
  public function method() {
     $var1 = "hello";
     $var2 = "world";
     $this->load->model('mymodel', 'model', TRUE);
     $this->mymodel->insert_entry($var1, $var2);
  }
}
?>

I used error_logging and I found that the
Code:
$this->db->insert('table', $this);
in Mymodel does not execute and I can't figure out why. I've read CodeIgniter's Models tutorial and this seems like it should work. (http://ellislab.com/codeigniter/user-gui...odels.html) I was wondering if someone could point out what I did wrong.
#2

[eluser]solid9[/eluser]
not test but try this,
Code:
<?php
class Mymodel extends CI_Model {
public $data = array(
                   'field1' => '',
                   'field1' => '',
                   'field1' => ''
    );

  function __construct() {
     parent::__construct();
  }

  function insert_entry($field1, $field3) {
    $this->data['field1'] = $field1;
    $this->data['field2'] = $this->input->post('field2');
    $this->data['field3'] = $field3;

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


<?php
class MyController extends CI_Controller {
  public function method() {
     $var1 = "hello";
     $var2 = "world";
     $this->load->model('mymodel', 'model', TRUE);
     $this->mymodel->insert_entry($var1, $var2);
  }
}
?>
#3

[eluser]dmisterct[/eluser]
That didn't seem to fix my problem.
#4

[eluser]dmisterct[/eluser]

If it helps here is the software stack on my OS

PHP 5.3.3
CentOS not sure which version.
#5

[eluser]InsiteFX[/eluser]
Insert expects and associate array or a class object!
Code:
public function insert_entry($field1, $field3)
{
    $data = array(
        'field1' => $field1,
        'field2' => $this->input->post('field2'),
        'field3' => $field3
    );

    $this->db->insert('table', $data);
}
#6

[eluser]dmisterct[/eluser]
I'm just wondering, this database and the tables already existed. I'm actually trying to take an existing application and convert it into a CodeIgniter based application. Does having an existing database with certain existing fields prevent CodeIgniter's data models to combine?

So for example from a mysql vardump I get the previously created table which I am trying to model:

Code:
CREATE TABLE `table` {
    `field1` INT(18) NOT NULL AUTO_INCREMENT,
    `field2` INT(18) NOT NULL,
    `fielrd3` VARCHAR(50) DEFAULT NULL
}

Also @ InsiteFX, I thought that
Code:
$this
meant this class. Is that not a class object of the current class? If not then the tutorial on CodeIgniter's Model page is misleading.

I did try using an associate array and it does not work. I can only assume my problem has to deal with the database itself. I am hoping if anyone could give some insight as to whether or not adding to an existing database with an existing schema affects how I should insert using CodeIgniter's data models.
#7

[eluser]dmisterct[/eluser]
Ok I figured out what was wrong, I misspelled one of the field names. It was such a small typo.




Theme © iAndrew 2016 - Forum software by © MyBB