We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model Extension

Is it possible to extend Models somehow like:

class BaseModel extends Phalcon\Mvc\Model{

    public $id,$name1;

    public function getSource (){
        return "base";
    }

    public function columnMap(){
        return array(
            "id" => "id",
            "name" => "name1"
        );
    }
}

class ExtendedModel extends BaseModel{

    public $id,$baseModelId,$name2;

    public function getSource (){
        return "normal_model";
    }

    public function columnMap(){
        return array(
            "id" => "id",
            "name" => "name2",
            "baseModelId" => "baseModelId"
        );
    }
}

$models = ExtendedModel::query()
    ->leftJoin("BaseModel","BaseModel.id = ExtendedModel.baseModelId")
    ->where("ExtendedModel.id < 12")
    ->execute();

foreach($models as $medium){
    var_dump($medium->toArray());
}


98.9k

Yes, are you getting trouble with this?



9.2k
edited Mar '14

yes, i can not access name1 if I request something like:

foreach($models as $medium){
    var_dump($medium->name1);
}

by the way, in my original model the table rows have different names. so "name1" and "name2" are not both "name" in my tables



9.2k
Accepted
answer
edited Mar '14

The only chance I saw was to manually handle the problem by adding the following

public function afterFetch(){
        /** @var BaseModel $parent  */
        $parent = BaseModel ::findFirst($this->baseModelId);
        if($parent instanceof BaseModel ){
            $this->name1= $parent->name1;
        }
    }

But I think even the baseModelId shouldn't be neccessary