Question

I have 3 models, Where

Basemodel
Model_A
Model_B

In Basemodel i have initialize function setting a property value

In Model_A i don't have initialize function, if i return anything from model 
it will be successful

In Model_B i have initialize function, if i return anything from this model 
its not returning anything. No Error, Nothing, Just Blank.

Base Models With initialize()

class Basemodel extends \Phalcon\Mvc\Model {

    protected $name;

    function initialize() {
        $this->name = 'MyApplication';
    }

}

Model A Without initialize():

class Model_a extends Basemodel {
    function test() {
        return $this->name; // WORKING
    }
}

Model B With initialize():

class Model_b extends Basemodel {
    public $b;
    function initialize() {
        $this->b = 'someName';
    }

    function rawtext() {
        return $this->name;  // NOT WORKING
    }
}

If remove initialize() from Model_B, Then its returning the value successfully

Was it helpful?

Solution

This happens because in child classes you are overriding BaseModel initialize method.

If you want to extend initilize in child class you should call initialize from parent class - here BaseModel.

Model A/B should be:

class Model_b extends Basemodel {
    public $b;
    function initialize() {
        parent::initialize(); // Call parent's class initialize
        $this->b = 'someName';
    }

    function rawtext() {
        return $this->name;  // SHOULD WORK
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top