I have the following in one of my child active record class. To init my AR, I call $this->publicMode = true, in __construct(), and then the program just runs for a while and gives back nothing.
private $_publicMode;
public function setPublicMode($publicMode)
{
$publicMode = (bool)$publicMode;
$this->_publicMode = $publicMode;
if($publicMode)
$this->onPublicModeOn(new CModelEvent($this));
else
$this->onPublicModeOff(new CModelEvent($this));
$this->onPublicModeChange(new CModelEvent($this));
}
public function getPublicMode()
{
return $this->_publicMode;
}
update: I’ve found the reason. Calling $this->publicMode inside __construct() when AR::model() is being called will invoke getMetaData(), and what getMetaData() does is call model() again to get the metadata, causing an infinite loop and causing php to time out.
So the solution to this is
public static function model($className = __CLASS__)
{
$model = parent::model($className);
$model->init();
return $model;
}
public function init()
{
$this->publicMode = false;
}
I think it would have made more sense if yii allows us to call init() in model() to do necessary preparations like above.