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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.