Ben
Ben

Reputation: 21249

Is there a way to use a single inheritance table as a foreign key table in propel?

In one project, I've got a generic Meta table that any other table can use to store additional data into.

The Meta table has a (target_type, target_id) pair of columns that reference the foreign table and foreign key the meta entry is about.

e.g.

The schema for Meta looks like this:

<table name="meta" phpName="Meta">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
    <column name="target_id" type="integer" required="true" />
    <column name="target_type" type="varchar" size="50" required="true" />
    <column name="code" type="varchar" size="50" required="true" />
    <column name="value" type="varchar" required="true" />
</table>

I know I can set a relationship in the donkeys table using foreign-key to the Meta table like so:

<table name="donkeys" phpName="Donkey">
    /* ...  */
    <foreign-key foreignTable="meta">  
        <reference local="id" foreign="target_id"/>  
    </foreign-key> 
</table>

But that doesn't fullfil the additional condition that target_type should be set to donkeys in that relationship.

So $donkey->getMeta() might actually return Meta info that has nothing to do with donkeys!

I can't see a way to set an additional condition in the foreign-key declaration in the schema. Is that correct?

I thought maybe inheritance could fix that, so I changed the Meta schema to this:

<table name="meta" phpName="Meta">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
    <column name="target_id" type="integer" required="true" />
    <column name="target_type" type="varchar" size="50" required="true" inheritance="single">
        <inheritance key="users" class="UserMeta"/>
        <inheritance key="donkeys" class="DonkeyMeta"/>
    </column>
    <column name="code" type="varchar" size="50" required="true" />
    <column name="value" type="varchar" required="true" />
</table>

But I can't seem to be able to set the relationship in foreign-key to link to the inherited subclass DonkeyMeta.

i.e. this doesn't work:

<table name="donkeys" phpName="Donkey">
    /* ...  */
    <foreign-key foreignTable="DonkeyMeta">  
        <reference local="id" foreign="target_id"/>  
    </foreign-key> 
</table>

Is there are a way to do this kind of relationship with a propel schema?

Upvotes: 1

Views: 145

Answers (1)

Jordan Kasper
Jordan Kasper

Reputation: 13273

There isn't, unfortunately. I've implemented the same setup as you have, and I've asked the creator of Propel the same thing (and he didn't have a good answer for me). The best I could do was to override/add some methods to the *Query classes for the various entities involved.

Upvotes: 1

Related Questions