0

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

N. francis
  • 75
  • 1
  • 12
  • 1
    Just a tip you don't need to have redirect like `redirect('http://localhost/kgmerchant/index.php/welcome');` if you set your base url in config.php `$config['base_url'] = 'http://localhost/kgmerchant/';` then autoload url helper you can then use redirect like `redirect(base_url('welcome'))` –  Nov 02 '17 at 11:04
  • can you show the errors? – prakash tank Nov 02 '17 at 11:06
  • @prakashtank, I've update with the error – N. francis Nov 02 '17 at 11:11
  • @wolfgang1983, Thanks – N. francis Nov 02 '17 at 11:14
  • The error message is clear: you can't to declare a new class with a name already used in your application. – prakash tank Nov 02 '17 at 11:22
  • I've updated the error message – N. francis Nov 02 '17 at 11:43
  • Please make sure you create a [mcve] and post the full error message you receive, including details which you don't understand. Since you've now posted two different errors here, it looks like you're still in the middle of debugging this, and can probably narrow down the problem further before asking for help. – IMSoP Nov 02 '17 at 11:44
  • @IMSoP, Thanks, the first error was from session, which I've fixed. – N. francis Nov 02 '17 at 11:51
  • @prakashtank, Please can you help with a solution? – N. francis Nov 02 '17 at 15:04
  • @N.francis : I hope you got the solution if not then please echo your query and check the output by executing `echo $this->db->last_query();` function. the error you are showing here is that you are getting array and you are passing it into the query. – prakash tank Nov 03 '17 at 04:23
  • Hi @prakashtank, I haven't found a solution yet, I'm using [https://gist.github.com/dazld/2174233](https://gist.github.com/dazld/2174233) as a guideline to implement a controller, but finding it challenging to implement. please could you use a bit of your time to review it. Thanks – N. francis Nov 03 '17 at 07:53

1 Answers1

0

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.

  • Hi, I just did that, but have an error, I've updated the error message – N. francis Nov 02 '17 at 11:42
  • You're calling `$this->closure->add($add_new);` where $add_new is an array, but then in the `add` method you're expecting 2 parameters and using them separately. Either pass the parameters separately to `add()` or change the method so that it uses the array items. – Christian Maioli M. Nov 02 '17 at 11:53
  • Thanks, I'm trying to achieve a closure table, Do you have a better way I could modify the model, I guess that's where bulk of the bug is. Thanks – N. francis Nov 02 '17 at 12:12