I must fill all fields on insertion of master table , otherwise An error occured on insert/saving pops up. Howvever, the record is saved in the master table.
Master table:
CREATE TABLE `inovasi` (
`inovasiId` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(300) DEFAULT NULL,
`categoryCode` int(11) DEFAULT NULL,
`clusterCode` int(11) DEFAULT NULL,
PRIMARY KEY (`inovasiId`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;
Trx table
CREATE TABLE `phone_numbers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`inovasiId` int(11) DEFAULT NULL,
`phone_type` varchar(255) DEFAULT NULL,
`phone_number` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;
Controller:
{
$crud = new grocery_CRUD();
$crud->set_theme('flexigrid');
$crud->set_table('inovasi');
$crud->display_as('clusterCode','Cluster');
$crud->display_as('categoryCode','Category');
$crud->display_as('phone_number','E-Collab');
$crud->set_subject('MSTE');
$crud->set_relation('clusterCode','cluster','description');
$crud->set_relation('categoryCode','category','{descriptionMalay}/{descriptionEn}',
array('status' => '1'));
$crud->fields('inovasiId','title','categoryCode','clusterCode','phone_number');
$crud->field_type('inovasiId', 'invisible');
$crud->columns('title','categoryCode','clusterCode');
$crud->callback_field('phone_number', array($this, 'phone_number'));
$crud->callback_read_field('phone_number',array($this,'read_phone_number'));
$crud->callback_after_insert(array($this, 'save_phone_number'));
$crud->callback_after_update(array($this, 'update_phone_number'));
$crud->callback_after_delete(array($this,'delete_master'));
$layout = $crud->render();
$outputs = $layout->output;
$js['js_files'] = $layout->js_files;
$css['css_files'] = $layout->css_files;
$page_layout = $this->session->userdata('Page_Session');
$mste['output'] = $outputs;
$mste['header'] = $page_layout['header'];
$mste['js_files'] = $js['js_files'] ;
$mste['css_files'] = $css['css_files'] ;
$this->_mste_output($mste);
....
function phone_number($value = '', $primary_key){
$this->db->where('inovasiId', $primary_key);
$rows = $this->db->get('phone_numbers');
$numbers=$rows->result_array();
$html = '
<script src="mste.js"></script>
<table id="phone_number">
<tr> <th>type</th><th>Phone number</th> </tr>';
foreach ($numbers as $number){
if (!empty($number["phone_number"]) )
$html.= '<tr><td><input name="phone_types[]" type="text" value="'.$number["phone_type"].'" >
</td><td><input name="phone_numbers[]" type="text" value="'.$number["phone_number"].'" >
</td><td><button class="remove_number btn btn-danger">remove</button></td></tr>';
}
$html .='
<tr><td><input name="phone_types[]" type="text" ></td><td><input name="phone_numbers[]" type="text" ></td><td><button class="remove_number btn btn-danger">remove</button></td></tr>
</table>
<button type="button" id="add_number" class="btn btn-info" style="width: 100px; margin:20px 0px; ">add</button>
';
return $html;
}
function save_phone_number($post_array, $primary_key){
//first we delete all the old records since we're going to re-insert them back!
// $this->db->delete('phone_numbers', array('inovasiId' => $primary_key));
$i = 0;
$data1 = array();
//if ($this->db->affected_rows()){
while (!empty($post_array["phone_numbers"][$i]) ){
$phone = array(
'phone_type' => $post_array["phone_types"][$i],
'phone_number' => $post_array["phone_numbers"][$i],
'inovasiId' => $primary_key
);
array_push($data1, $phone);
$i++;
}
$this->db->insert_batch('phone_numbers', $data1);
//log_message('debug', 'SQL: '.$this->db->last_query());
// }
// return FALSE;
// $this->db->insert_batch('phone_numbers', $data1);
unset($post_array['phone_numbers']);
}
function update_phone_number($post_array, $primary_key = null){
//first we delete all the old records since we're going to re-insert them back!
$this->db->delete('phone_numbers', array('inovasiId' => $primary_key));
$i = 0;
$data1 = array();
if ($primary_key !== null){
while (!empty($post_array["phone_numbers"][$i]) ){
$phone = array(
'phone_type' => $post_array["phone_types"][$i],
'phone_number' => $post_array["phone_numbers"][$i],
'inovasiId' => $primary_key
);
array_push($data1, $phone);
$i++;
}
$this->db->insert_batch('phone_numbers', $data1);
//log_message('debug', 'SQL: '.$this->db->last_query());
//return TRUE;
}
//else {return FALSE;}
// $thi
// return TRUE;
}