codeigniter: closure table - add descendant

189 Views Asked by At

I'm trying to add a new descendant, but having difficulties achieving it, it displays some error, Would be grateful if you could take time to review what I've done thus far.

Here's

Controller

public function index() {
        $this->load->view('closure_view');
    }
    public function add() {

        $add_new = array(
            'ancestor'      => $this->input->post('ancestor'),
            'descendant'        => $this->input->post('descendant'),
            'lvl'   => $this->input->post('lvl'),
            'id'    => $this->input->post('id')
        );

        $cust_id = $this->closure->add($add_new);
        redirect('http://localhost/kgmerchant/index.php/welcome');
    }

Model

public $table;
    public $closure_table = 'closures';
    public function __construct($table_name = NULL, $closure_table = NULL){
        parent::__construct();
        $this->table = $table_name;
        if ($closure_table !== NULL) {
            $this->closure_table = $closure_table;
        }
    }
public function add($node_id, $target_id = 0) {
        $sql = 'SELECT ancestor, '.$node_id.', lvl+1
                FROM '.$this->closure_table.'
                WHERE descendant = '.$target_id.'
                UNION
                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="home" 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" required />
            <input placeholder="id" type="hidden" name="id" value="" />    
            <input type="submit" value="Okay" />   

        </form>

Thanks.

Error

Message: Array to string conversion

1

There are 1 best solutions below

3
Christian Maioli M. On

Your model can't be called "Closure" because that's a reserved name in PHP. Change the name to something else and it should work.