HAS_MANY through vs. MANY_MANY

Hey everyone, I was wondering what the difference between the HAS_MANY through and the MANY_MANY relationship was. My original understanding was that HAS_MANY through was for MANY_MANY relationships where the linking table also had some additional data in it, and that the MANY_MANY relationship was for instances where the linking table only has two fields which are foreign keys to the tables it is linking.

Is this the same case in the Yii framework?

Can anyone provide me with some examples where I’d want to use MANY_MANY and HAS_MANY through?


Conceptually, from DB point of view, both describe two kinds of relationship between entities:

  • has_many , aka ‘one to many’ : one given entity (e.g. a given Author) may be related to several instances of another entity (e.g. Posts) (One given Author may write many Posts)
  • many_many , aka ‘many to many’ : a given entity of each side can be related to many entities of the other side (e.g. : Posts and Categories: a Post may have many Categories, and a Category have many Posts). (In the has_many above, one given Post has only one Author)

(Cardinality is the name for the concept behind how many entities are involved on each side of a relationship)

With mysql and co, has_many can be realized with a reference in the ‘many’ table: with the example above, the table for Post will have a reference to the Author (through an author id).

The relationship many_many can not be realized that way: it is realized with a dedicated table, typically containing two fields; an id for each entity involved in the relationship (e.g.: a table named post_has_category or PostCategories, or whatever, having an post_id and a category_id; see http://www.i-avingto…blog_tables.png)

For more information, search for "entity relationship model". Here some first results :



Hi Accelm

Have been trying to find the answer to exactly the same thing.

So far it seems that has_many through is more versatile than many_many but many_many simpler as pivot model not needed. In a many to many relationship it appears I can use either self::MANY_MANY to get related data via the pivot table without creating the pivot model. It appears the pivot model is needed with self::HAS_MANY through. On the other hand HAS_many through appears to get at any data via another model regardless of whether that entails a many to many relationship.

Correct me if I am wrong - still learning.

Thanks, Jean-Marie. I understand the difference between a HAS MANY versus a MANY MANY relationship as far as relational databases go. I am looking specifically at the usage of the HAS MANY THROUGH in Yii as a way to set up a relation, which I think andrew1 is on the same page as. A MANY MANY relationship can also be modeled as a HAS MANY THROUGH relationship in Yii, and I was just wondering if there are any good examples of when I’d want to use one versus the other.

Andrew mentioned that the pivot model is needed in a HAS MANY THROUGH which is a good point. Has anyone else noticed any important differences or reasons you would want to use one over another?