Jump to content

Using codeigniter activerecord properties within a static method


johnmerlino

Recommended Posts

Hey all,

 

In my controller I have this:

 

public function __construct(){
		parent::Controller();
		$this->load->model('home');
	}

	public function index(){
		$result = Home::find('all');

	}

 

 

In model this:

 

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

	public static function find($param){
		if(is_null($param) || $param === 'all'){
			$resources = $this->db->get('home'); //but I can't do self::db - because db is not a static property of the Home class, which is what self is pointing to.
			return $resources->result();
		}
		return null;
	}
}

 

Since $this property is a reference to an object (a new instance), and a static method is not an instance, but a class method, I must use self and not this in the function block. However, the db property codeigniter provides cannot be called as a static property, since it was not declared as such. So when I get the error:

 

Fatal error: Access to undeclared static property: Home::$db

 

or

 

Fatal error: Using $this when not in object context

 

I am not sure what to do.

 

Thanks for response.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.