Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override model's constructor correctly in CakePHP

I have troubles with testing Model in CakePHP 2.0 and it seems the problem is on the model's constructor.

public function __construct(){
    parent::__construct(); 
    $this->_pagi_cuantos = 2;
}

Even if I delete all its content, I still getting problems trying to run the test.

Mark Story told me:

if you have a constructor make sure you're overriding the constructor correctly. Failing to do so will cause errors like this.

What do I wrong?

like image 493
Alvaro Avatar asked Nov 27 '22 17:11

Alvaro


2 Answers

why don't you look into the core code its open source after all: https://github.com/cakephp/cakephp/blob/2.1/lib/Cake/Model/Model.php#L653

so for all your models:

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
}
like image 57
mark Avatar answered Dec 06 '22 19:12

mark


Rather than override the constructor, how about using beforeFilter() for controllers or the before methods for the Model such as beforeFind(), beforeValidate(), etc.

like image 42
Kris Avatar answered Dec 06 '22 19:12

Kris