codeigniter: closure table - add descendant

天涯浪子 提交于 2019-12-20 07:14:06

问题


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:


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.



来源:https://stackoverflow.com/questions/47073695/codeigniter-closure-table-add-descendant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!