write a controller class from a model in a closure table

67 Views Asked by At

I'm trying to write a controller class from a model class for a closure table.

Trying to get my head into it, but it seems difficult. I want to insert entries into a closure table.

Here's the 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 ALL SELECT '.$node_id.','.$node_id.',0';
    $query = 'INSERT INTO '.$this->closure_table.'(ancestor, descendant, lvl) ('.$sql.')';
    $result = $this->db->query($query);
    return $result;
}

Error message

"Syntax error at UNION"

0

There are 0 best solutions below