Leonard Washington
Leonard Washington

Reputation: 37

Codeigniter Model function not working

I am starting out with codeigniter, but the documentation is horribly written and doesn't actually work with the new version. My issue is that I cannot call a function within a model.

Here is my model: User.php

 <?php

    class User extends CI_Model {

        function __construct()
        {
            parent::__construct();
        }
    }
    function test($x)
    {
        return $x;
    }

    ?>

And my controller: welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->model('User');
        echo $this->User->test('darn');
        $this->load->view('welcome_message');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

?>

Upvotes: 0

Views: 1082

Answers (1)

gen_Eric
gen_Eric

Reputation: 227200

Check your {}s, your test function is outside your User class. Move it inside the class, then $this->User->test() will work.

<?php
    class User extends CI_Model {

        function __construct()
        {
            parent::__construct();
        }

        function test($x)
        {
            return $x;
        }
    }
?>

There's nothing "horrible" about the documentation.

Upvotes: 1

Related Questions