Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Model Extension

Is it possible to extend Models somehow like:

```php 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()); }

```



83.4k

Yes, are you getting trouble with this?



5.8k
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



5.8k
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