Welcome Guest, Not a member yet? Register   Sign In
Issues in loading models
#1

[eluser]Dvinge[/eluser]
Hello.
I've been wandering on forums searching for a solution. I cannot use Models correctly when using $this.

Code:
Controller:
<?php

class Forum extends CI_Controller {
    function __construct(){
        parent::__construct();
    }

    function index(){
        $this->load->model('forummodel');
        
        // Works
        $CI =& get_instance();
        echo $CI->forummodel->a('a');

        // Doesn't work
        echo $this->forummodel->a('a'); // line 16
    }
}

?>

Model:
<?php

class Forummodel extends CI_Model{
    function __construct()
    {
        parent::__construct();  
    }

    function a($a){
        return $a;
    }
}

?>

It works when I'm using get_instance(); but without im getting this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Forum::$forummodel
Filename: controllers/forum.php
Line Number: 16

Hope someone got a solution for this problem :-)

EDIT:
Code:
$this->forum->forummodel->a('a'); // works too
$this->Forum->forummodel->a('a'); // doesn't work
#2

[eluser]Derek Allard[/eluser]
Welcome to CI Dvinge.

Your model looks fine (except the constructor note below also applies to it), but there are a few things we can clean up in your controller.

Firstly, you don't need the constructor there, and I'd recommend you remove it (read Constructor-structor, what's your function?).

Next up, you don't need to invoke the $CI super object in your controller. I'd make your whole controller this:

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

class Forum extends CI_Controller {

    function index(){
        $this->load->model('forummodel');
        echo $this->forummodel->a('foo');
    }

}

*Notes: removed contructor, modified the opening '<?php' statement, removed '?>'
#3

[eluser]Dvinge[/eluser]
Thanks for the cleanup, but the real problem is still alive.

When I'm loading forummodel, it's not showing up in $this, but in $this->forum. I also tried a older version, but I'm getting the same issue.
#4

[eluser]Edmundas KondraĊĦovas[/eluser]
Are you using a clean version of CodeIgniter?
#5

[eluser]Derek Allard[/eluser]
[quote author="Dvinge" date="1299947375"]When I'm loading forummodel, it's not showing up in $this, but in $this->forum.[/quote]

Wait, so when you use
Code:
$this->forummodel->a('foo');
it works for you, but you want to be able to use
Code:
$this->a('foo');

If I'm reading that correct, then its not really the way CI is intended to be used. CI is intended that you need $this->{library}->{function}.

You can rename it from 'forummodel' to 'forum' if you want when you load it, but you can't exclude it.
#6

[eluser]Dvinge[/eluser]
[quote author="Derek Allard" date="1299964960"][quote author="Dvinge" date="1299947375"]When I'm loading forummodel, it's not showing up in $this, but in $this->forum.[/quote]

Wait, so when you use
Code:
$this->forummodel->a('foo');
it works for you, but you want to be able to use
Code:
$this->a('foo');

If I'm reading that correct, then its not really the way CI is intended to be used. CI is intended that you need $this->{library}->{function}.

You can rename it from 'forummodel' to 'forum' if you want when you load it, but you can't exclude it.[/quote]

No, i can't do $this->formmodel->a('foo'); as i tried to explain in the first post, but I wish to use $this->formmodel->a('foo');

I'm using a clean CI 2.0 yes.

EDIT: Found the issue...
I was autoloading a Forum class from my library which did something to my Forum controller i guess.

Thanks for looking at it :-)




Theme © iAndrew 2016 - Forum software by © MyBB