Welcome Guest, Not a member yet? Register   Sign In
Problem with extended Model
#1

[eluser]christian studer[/eluser]
Good morning,

I'm having a problem with an extended Model. My code looks like this (No autoloading of any kind):

Code:
application/libraries/MY_Model.php:
class MY_Model extends Model {

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

    public function loadSimpleTable(...)
Code:
application/models/hubconfig.php:
class Hubconfig extends MY_Model
{
    private $myhubconfig = array();
    
    // Constructor loads configuration into local array
    public function __construct()
    {
        parent::__construct();        
        $this->myhubconfig = $this->loadSimpleTable('hubconfig', 'key', 'value');
    }
    
    public function getValue($key) {
        return $this->myhubconfig[$key];
    }
}

This loads fine, as far as I can see.

But then in my view I'm trying to load a value from hubconfig and it all falls apart:

Code:
<?php
$this->load->model('hubconfig');
?>

    <h1>Welcome to &lt;?php echo $this->Hubconfig->getValue('hubname'); ?&gt;</h1>

This generates first a notice:

Message: Undefined property: CI_Loader::$Hubconfig (On the Welcome-line)

Then a fatal error on the same line:

Fatal error: Call to a member function getValue() on a non-object in U:\Reference\Workspace\cronhub\application\views\login.php on line 12

Can anybody tell me what's wrong here?

(So far the only workaround I've found was to instanciate MY_Model and Hubconfig myself, without using CIs load-methods.)
#2

[eluser]davidbehler[/eluser]
I think the second error is just a subsequent fault to the first one.

Have you tried loading the model in your controller instead of the view?

Maybe you can try this when loading your view:
Code:
$this->load->model('hubconfig');
$data['hubname'] = $this->Hubconfig->getValue('hubname');
$this->load->view('view_name', $data);

And in your view:
Code:
<h1>Welcome to &lt;?php echo $hubname; ?&gt;</h1>
#3

[eluser]TheFuzzy0ne[/eluser]
In order to stay compatible with PHP 4, CI constructors are named according to their class name. If you call the model constructor like this:

Code:
parent::Model();

you may find it works as expected.

EDIT: I just tested it, and there's no reason why __construct shouldn't work. That is, of course, so long as it really is PHP 5 you're running, and not PHP 4.
#4

[eluser]TheFuzzy0ne[/eluser]
How are you loading your view file?
#5

[eluser]christian studer[/eluser]
@waldmeister: Loading in the controller doesn't change a thing, the error occurs simply there. Like I said: The model loads fine.

@TheFuzzy0ne: We're working with PHP5. If I change my constructors back to PHP4-style, the same error occurs.

The view is loaded normally with a simple $this->load->view('welcome').
#6

[eluser]TheFuzzy0ne[/eluser]
Sorted!

Please try changing this line:

Code:
<h1>Welcome to &lt;?php echo $this->Hubconfig->getValue('hubname'); ?&gt;</h1>

to this:

Code:
<h1>Welcome to &lt;?php echo $this->hubconfig->getValue('hubname'); ?&gt;</h1>

Note that the model object's name is lowercase. It's these little typos that cause such big problems.

If you want to use a model name with the first letter as uppercase, you will need to pass it to the loader as the second parameter:

Code:
$this->load->model('hubconfig', 'Hubconfig');

Hope this helps. Smile
#7

[eluser]christian studer[/eluser]
Sorry, it doesn't. It is no problem of lower/uppercase, I just tried about every combination I could think of.

In fact the load-function is case insensitive, the model is then aviable as $this->Classname and has usually the same name and case as the class name (Here: Hubconfig).
#8

[eluser]xwero[/eluser]
The parent constructor in the MY_model class constructor needs to be parent::Model(). The Model class has no __construct method.

The MY_ prefix classes extends the base class in a hidden way and are autoloaded. So your actual model classes have to extend the model class instead of the MY_ class.

If you make those changes everything should work.
#9

[eluser]TheFuzzy0ne[/eluser]
Please append "_model" to the file name.
Code:
hubconfig_model.php

EDIT: Scrap that. Now I'm being a muppet...
#10

[eluser]TheFuzzy0ne[/eluser]
[quote author="christian studer" date="1234205680"]Sorry, it doesn't. It is no problem of lower/uppercase, I just tried about every combination I could think of.

In fact the load-function is case insensitive, the model is then aviable as $this->Classname and has usually the same name and case as the class name (Here: Hubconfig).[/quote]

Perhaps I am misunderstanding what you are saying.

Controller - APPPATH/controllers/test.php:
Code:
&lt;?php

class Test extends Controller {

    function Test()
    {
        parent::Controller();
        $this->load->model('test_model');
    }
    
    function index()
    {

    }
    
    function test1()
    {
        echo $this->test_model->get();
        // Works great.
    }
    
    function test2()
    {
        echo $this->Test_model->get();
        // Doesn't work. Throws the error you're seeing.
    }
}

Model - APPPATH/models/test_model.php
Code:
&lt;?php

class Test_model extends Model {

    function Test_model()
    {

    }
    
    function get()
    {
        return "test string";
    }

mydomain.tld/test/test1 - works as expected.
mydomain.tld/test/test2 - throws an error like you're seeing...

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Test::$Test_model

Filename: controllers/test.php

Line Number: 24

Fatal error: Call to a member function get() on a non-object in /var/www/html/system/application/controllers/test.php on line 24




Theme © iAndrew 2016 - Forum software by © MyBB