CodeIgniter Forums
How can I use active record to insert DEFAULT VALUE? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How can I use active record to insert DEFAULT VALUE? (/showthread.php?tid=41909)



How can I use active record to insert DEFAULT VALUE? - El Forum - 05-20-2011

[eluser]fell0206[/eluser]
Hello everybody, I have a problem with the active record of insert.
My table have a "Root" of column(not null), it have a default value(0), I create a class, and I use "$this->db->insert('mytable',$data);" to insert the record, but when the variable "Root" is null, then the error will occur.
How can I fix this error!! Thanks~

MyTable:
Code:
CREATE TABLE IF NOT EXISTS `test` (
  `A` int(1) NOT NULL,
  `Root` int(1) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

MyClass:
Code:
class MyClass{
   public $A;
   public $Root;
}

Controller:
Code:
...
$myClass=new MyClass();

$this->load->model('test_model');
$myClass->$A=$this->input->post('A');
$this->test_model->add($myClass);
...

Model:
Code:
...
function add($myClass){
   $this->db->insert('test',$myClass);
}
...



How can I use active record to insert DEFAULT VALUE? - El Forum - 05-21-2011

[eluser]theprodigy[/eluser]
Not sure if what you show in your code is copy/pasted, but shouldn't
Code:
...
$myClass->$A=$this->input->post('A');
...

be

Code:
...
$myClass->A=$this->input->post('A'); //Remove the $ from $A in $myClass->$A
...

Also, what is the output of this?

Code:
function add($myClass){
   //$this->db->insert('test',$myClass);
   echo $this->db->insert_string('test', $myClass);
}



How can I use active record to insert DEFAULT VALUE? - El Forum - 06-06-2011

[eluser]fell0206[/eluser]
Sorry, it's my fault.
In my file it is correct:
Code:
$myClass->A=$this->input->post('A');

About insert string, if variable not set, it will be NULL.(if column have CURRENT_TIMESTAMP and AUTO_INCREAMENT, it work corrent).
How can I fix it, thank you~


How can I use active record to insert DEFAULT VALUE? - El Forum - 06-06-2011

[eluser]theprodigy[/eluser]
Quote:About insert string, if variable not set, it will be NULL.
I'm looking for the ACTUAL output of the string, not 'what it will be'. Please run the code, and copy/paste the output of it.