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

Models shared table

Hi. I was wondering it it is possible to use same table for different Models.

I have a table called credits Table Structure:

Now I want to have 2 Models:

class Cast extends Model{

    public function initialize(){

        $this->setSource("credits");
        // "credits" source but only where " credits.type = 'cast' " 

        // should only get columns: ['id', 'person_id', 'character', 'order']
    }

}
class Crew extends Model{

    public function initialize(){

        $this->setSource("credits");
        // "credits" source but only where " credits.type = 'crew' " 

        // should only get columns: ['id', 'person_id', 'job', 'department']
    }

}

There is no built-in way to do it but if you can force it, using independent column mapping and overloading find() and findFirst()

class Cast extends Model{

    private $type = 'cast'; // force cast type and do not change it

    public function initialize(){
        $this->setSource("credits");
    }

    public function columnMap()
    {
        return [
            'id'       => 'id',
            'person_id' => 'person_id',
            'character' => 'character',
            'order' => 'order',
        ];
    }

    // then overwrite methods find and findFirst to use allways type = 'cast'
}

Good luck