Welcome Guest, Not a member yet? Register   Sign In
$this->load->model('Blog')
#11

[eluser]squall15[/eluser]
Just wanna point out that I am also having this issue.

I developed a project in CI before using the 1.7.1 and everything is working fine. Using the 1.7.2, I adopted some of codes I used from previous version. This is giving me a headache.
#12

[eluser]squall15[/eluser]
I figured out where mine is giving error at..

So, I have a file in my library called "MY_Controller" and that extends from Controller. All my controller in the controllers folder are extended from the custom MY_Controller.

Once I set it to extend to the original Controller, everything is working fine.
#13

[eluser]Michael Wales[/eluser]
squall - yours should work as long as you call Controller's constructor from MY_Controller's constructor:

Code:
parent::Controller();
#14

[eluser]squall15[/eluser]
[quote author="Michael Wales" date="1265342208"]squall - yours should work as long as you call Controller's constructor from MY_Controller's constructor:

Code:
parent::Controller();
[/quote]

Thanks for suggestion. But I did.

Here's how I did it.

Controller:
Code:
class User extends MY_Controller {
  public function User() {
    parent::MY_Controller();
    $this->load->model('user_model');
    $this->user_model->validate_user();        // Giving me error here...
  }
}

Extended Controller:
Code:
class MY_Controller extends Controller {
  public function MY_Controller() {
    parent::Controller();
  }
}

Model:
Code:
class User_model extends Model {
  public function User_model() {
    parent::Model();
  }

  public function validate_user() {
    echo '123';
  }
}
#15

[eluser]danmontgomery[/eluser]
Let's not hijack this thread

Shouldn't this:

Code:
application/models/blog.php
be
Code:
site1/application/models/blog.php
?
#16

[eluser]ignitian[/eluser]
Are you suggesting that...

Code:
|---------------------------------------------------------------
| APPLICATION FOLDER NAME
|---------------------------------------------------------------
|
| If you want this front controller to use a different "application"
| folder then the default one you can set its name here. The folder
| can also be renamed or relocated anywhere on your server.
| For more info please see the user guide:
| http://ellislab.com/codeigniter/user-guide/general/managing_apps.html
|
|
| NO TRAILING SLASH!
|
*/
$application_folder = "application";

Should be :

Code:
$application_folder = "site1/application";

Maybe a clue...this system folder not suppose to be in the path of the application.
Code:
Warning: require(../system/site1/application/config/constants.php) [function.require]: failed to open stream: No such file or directory in C:\wamp\www\system\codeigniter\CodeIgniter.php on line 52

Fatal error: require() [function.require]: Failed opening required '../system/site1/application/config/constants.php' (include_path='.;c:\php\includes;c:\php\includes\class') in C:\wamp\www\system\codeigniter\CodeIgniter.php on line 52

i dont have this error if i write :

Code:
$application_folder = "application";

2nd EDIT

application is in the current directory. But system is one level higher and it is at the same level as site1

Code:
/*
|---------------------------------------------------------------
| MY SYSTEM FOLDER NAME
|---------------------------------------------------------------
|
| This variable must contain the name of your "system" folder.
| Include the path if the folder is not in the same  directory
| as this file. (index.php).
|
| NO TRAILING SLASH!
|
*/
$system_folder = "../system";
#17

[eluser]ignitian[/eluser]
Resolve !

Solution

$this referring to the current controller Welcome.

It was causing a issue using $this referring to the other class that have the same parent Controller.

Code:
function get_last_news()
{
  $this->load->model('Blog'); // NOW WORKING !
  $data['query'] = $this->Blog->get_last_ten_entries();
  return $data['query'];
}

Other explanations...

My own libraries contains a class Template Extends Controller that conflicting with the current class Welcome Extends Controller.

This issue was solved by NOT using Extends Controller.

Here's the new Class Template without using Extends Controller but CI instance when it need.

in : application/libraries/Template.php
Code:
class Template
{
    private $template = 'template';
    private $CI = '';
    
    function __construct()
    {
        $this->CI =& get_instance();
    }
    
    public function show( &$data )
    {
        $this->CI->load->helper('url');
        $this->CI->load->view($this->template,$data);
    }
    
    public function set($template)
    {
        $this->template = $template;
    }
}

in : application/controllers/welcome.php
Code:
class Welcome extends Controller
{
    // @var (Template)$template

    function __construct()
    {
        parent::Controller();
        $this->load->library('template');
        $this->load->database('blog');
    }

    function index()
    {
        $this->template->set('template3');
    .
    .
    .
    function get_last_news()
    {
        $this->load->model('Blog'); // NOW WORKING !
        $data['query'] = $this->Blog->get_last_ten_entries();
        return $data['query'];
    }
}

Using the above $this->template become a member of Welcome Controller as a Template Object.
Like class Model there is no need to use MY_Controller Extends Controller since you dont need to overload CI.


PHP 5.3 CI 1.7.2
Thanks all.
#18

[eluser]Unknown[/eluser]
I am having the same problem -- when I try to load a model FROM FUNCTION ie:

Code:
<?php

class MyModel extends Model {
...
public function doSomething(){
  $this->load->model('ModelToBeLoaded'); // Does not work
  $this->ModelToBeLoaded->someFunction();
  ...
}
then I get an error message like above:
Code:
Undefined property:  MyModel::$ModelToBeLoaded

However, when I load the exactly the same model from CONSTRUCT, then it works:

Code:
<?php

class MyModel extends Model {

function MyModel (){
  parent::Model;
  $this->load->model('ModelToBeLoaded'); // Now it works
}
...
public function doSomething(){
  $this->ModelToBeLoaded->someFunction();
  ...
}

I have spent hours to discover it...
It must be bug! At least it is very unexpected behavior and I don't want to load all function-specific models always...

EDIT:
By the way, I'm not in Controllers but in Models. My model structure is rather complicated and the problem of "multiple inheritance" may be the reason. The above-mentioned error was thrown to me, when I tried to call the model from my class trough Factory model (i.e. I am using Factory Pattern here). The Factory itself passed all unit tests, but the call to the Factory from my third model was giving a headache...




Theme © iAndrew 2016 - Forum software by © MyBB