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 inheritance

Hi everybody !

first of all, sorry in advance for my poor English...

I'm trying the inheritance of Models in Phalcon, but it don't work...

Explanation :

1 model named 'Entity' 1 model named 'User'

a User is an Entity, so the 'Entity' class is th mother of the 'User' class

the problem with an exemple :

$u = User::findFirst('identifiant = "toto"'); works for the User's datas, but don't retrieve the Entity's datas of this User...

How can we make the link between a Model mother class and his Model Child ?

edited Oct '15

Hmm... hope i understan u. So... if u mean that User has Entity and they are 2 separated models... then first u must specify relations between them (see documentation) something like :

    // This is in initalize function in User model
    // id is from User model
    $this->hasOne("id", "\Models\Entity", "id", array(
    'alias' => 'Entity'
    ));

Then, when u need to get entity for user:

    $user = User::findFirst();
    $entity = $user->Entity;

Hope that helps u. Good luck

PS: a read your post again (and again) and think i didnt understand u. First- why u need that type of inheritance? Need more details.

edited Oct '15

ok, I confirm my explantions are not very clear :-)

Perhaps with my code it will be clearly :

My Entity Model (in French) :

<?php
namespace API2\Modeles\Systeme;
use Phalcon\Mvc\Model;

class Entite extends Model {
    public $id;
    public $roleId;
    public function initialize()
    {
        $this->setSource('Entite');
        $this->belongsTo('roleId','API2\Modeles\Systeme\Role','id');
        $this->belongsTo('id','API2\Modeles\Systeme\Droit','entiteId');
    }
}
?>

My User Model (in French too) :

<?php
namespace API2\Modeles\Systeme;

class Utilisateur extends Entite
{
    public $entiteId;
    public function initialize()
    {
        $this->setSource('Utilisateur');
    }
}
?>

When I try :

$u = Utilisateur::findFirst('identifiant = "toto"');

echo $u->identifiant; // works (identifiant is a property in my table Utilisateur) echo $u->Id; // return NULL (id is a property in my table Entite)

Is there another way to make the link between the two models ? Can we make a single model for 2 tables ?



85.5k

i think this should answe your question.

https://docs.phalcon.io/en/latest/reference/models.html#understanding-records-to-objects

however you can try afterFetch function to accomplish your needs ?

Thats new logick for me (for models).

Can we make a single model for 2 tables ? i think not.

Just in case i will test your idea, but not sure that can be maked.

edited Oct '15

Thanks for your responses,

I think I'll try to change my Model's Design in order to not use Model inheritance but a simple link with hasOne()

Can I use $this->hasOne() in a model whitout having to use an other $this->hasOne() in the other Model ? (In my case, a User is an Entity, but an Entity can be other than a User)



2.1k
Accepted
answer
edited Oct '15

Obviously I can.

My Solution is to use afterFetch (Thanks to Izo) for copying the attributes of the Mother to the child :

<?php
namespace API2\Modeles\Systeme;
use Phalcon\Mvc\Model;

class Utilisateur extends Model
{
    public $id;
    public $roleId;
    public $actif;
    public $identifiant;
    public $motDePasse;

    public function initialize()
    {
        $this->setSource('Utilisateur');
        $this->hasOne('entiteId','API2\Modeles\Systeme\Entite','id',array('alias' => 'Entite'));
    }
    public function afterFetch(){
        $this->id = $this->Entite->id;
        $this->roleId = $this->Entite->roleId;
        $this->actif = $this->Entite->actif;
    }
}

Thanks a lot everybody !

You should use modelsManager and joins, this $this->Entite will made an aditional select.