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

Association tables needed?

Is it common practice in phalcon that association-tables are always used for all types of database relations?

(I understand it is needed for M-M relations. For 1-M relations it could be left out, but I'm not sure if that would break the build-in function to for example retrieve data.)

Hey,

you do not need association tables for 1-M relations. You can read more here: https://docs.phalcon.io/en/latest/reference/models.html#defining-relationships



1.6k

@Nikolay I read these docs, but it only has examples using association tables. that is why I asked. The docs are not really clear about this.

edited Oct '15

@allox yes in the docs the example covers the 3 table scenario (https://docs.phalcon.io/en/latest/_images/eer-1.png)

But you can simply use:

class Categories extends Model
{
    public $id;
    public $name;

    public function initialize()
    {
        $this->hasMany("id", "Products", "category_id");
    }
}

Where Products table has category_id and 1 category has many products.