I'm having issues implementing a controller for a closure table, I'm trying to INSERT a descendant.
Here's what I've done
Controller
public function add() {
$this->load->model('home_model', 'closures');
$node_id = $this->input->post('ancestor');
$target = array(
'descendant' => $this->input->post('descendant'),
'lvl' => $this->input->post('lvl'),
'id' => $this->input->post('id')
);
$cust_id = $this->closures->add($node_id, $target);
$this->index();
redirect(site_url());
}
Model
public function add($node_id, $target_id = 0) {
$sql = 'SELECT ancestor, '.$node_id.', lvl+1
FROM '.$this->closure_table.'
WHERE descendant = '.$target_id.'
UNION ALL SELECT '.$node_id.','.$node_id.',0';
$query = 'INSERT INTO '.$this->closure_table.'(ancestor, descendant, lvl) ('.$sql.')';
$result = $this->db->query($query);
return $result;
}
View
<form name="closures" method="post" action="<?php echo base_url().'index.php/home/add' ?>" >
<input placeholder="ancestor" type="text" name="ancestor" required/>
<input placeholder="descendant" type="text" name="descendant" required/>
<input placeholder="lvl" type="text" name="lvl" />
<input placeholder="id" type="hidden" name="id" value="" />
<input type="submit" value="Okay" />
</form>
Error messages 1. Array to string conversion 2. Syntax error on the model after UNION.
I'm having some challenges resolving the error messages