Zach Brown Thu Jun 09 20:36:25 -0400 2011

Subject: Model Constructor Function -- How to?

Hello. I'm trying to set up a way to initialize a memcache object within my model class, and I'm having a lot of trouble. This is what I have so far.

class rewrite extends ActiveRecord\Model {
static $db = 'click_tracking';
static $after_create = array('create_rewrite_memcache');
static $after_destroy = array('destroy_rewrite_memcache');
static $after_update = array('update_rewrite_memcache');

public $cache;
public function __construct()
{
parent::__construct();
$this->cache = new Memcache('localhost', 11211);
}
public function create_rewrite_memcache()
{
$cache->set(
$this->source,
array(
$this->isp_id,
$this->offer_id,
$this->mailer_id,
$this->destination
)
);
}
public function destroy_rewrite_memcache()
{
$cache->delete($this->source);
}
public function update_rewrite_memcache()
{
$cache->set(
$this->source,
array(
$this->isp_id,
$this->offer_id,
$this->mailer_id,
$this->destination
)
);
}
}
?>

The issue is that I can no longer access any of the object attributes. What is the best way to do this?

Thanks!


Daniel Lowrey Thu Jun 16 15:29:35 -0400 2011

If you're going to extend \ActiveRecord\Model::__construct() you still need to pass it the same parameters it would've received originally.

You called the parent constructor with no parameters:

1 // WRONG
2 parent::__construct();

If you need to extend __construct() in your child model you should be doing this:

1 public function __construct ($attributes=array(), $guard_attributes=TRUE, $instantiating_via_find=FALSE, $new_record=TRUE) {
2 
3     // Call the default Model constructor
4     parent::__construct ($attributes, $guard_attributes, $instantiating_via_find, $new_record);
5 
6     // Add your extended functionality here
7     $this->cache = new Memcache('localhost', 11211);
8 }

A look at the Docs shows us what the Model::_construct() needs to function
http://www.phpactiverecord.org/docs/ActiveRecord/Model#method__construct

(1-1/1)