Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Two-way but different relationship between models #633

Open
eggfriedrice opened this Issue Mar 19, 2013 · 2 comments

Comments

Projects
None yet
2 participants

Hi

I have an Article and an Event model. Articles are editable via my homebrew CMS. Events can have an Article associated with them so when you view an event you see the associated Article. I have set this up in my schema like this:

<table name="article" phpName="Article" idMethod="native">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
    <column name="title" phpName="Title" type="VARCHAR" size="255" required="false"/>

    <snip>
  </table>
<table readOnly="true" skipSql="true" name="event" phpName="Event" idMethod="native">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
    <column name="title" phpName="Title" type="VARCHAR" size="255" required="false"/>
    <snip>
    <column name="article_id" phpName="ArticleId" type="INTEGER" required="false"/>
    <snip>

    <foreign-key foreignTable="article">
      <reference local="article_id" foreign="id"/>
    </foreign-key>

  </table>

This all works fine, but I'd now like to amend the schema so that in addition to a specific one-to-one Event->Article relationship I also have other Articles that are "related" to Events. I figured this would be easy to add like this:

Add to Article schema...

<column name="related_event_id" phpName="RelatedEventId" type="INTEGER" required="false"/>

    <foreign-key foreignTable="event" phpName="RelatedEvent" refPhpName="RelatedArticle">
      <reference local="related_event_id" foreign="id"/>
    </foreign-key>

As I hoped, rebuilding my model this give me new RelatedArticle stuff and that works great, however it breaks the original one-to-one relationship. All the original Article functions are removed from the Event model. I no longer have the $event->getArticle() method, and the Query classes are also changed to remove the Article stuff.

What's happening here? Is there a way to get what I want?

Many thanks in advance,
Al

Have you tried adding an explicit phpName and refPhpName attributes in your Event table foreign-key?

Not sure what could be happening other than a name conflict somewhere... That said, if you can't get it working, a workaround may be to use a many-to-many relationship instead, with an extra field on the join table for marking "primary" article versus only a "related" article.

Let us know if you find any information on a bug, and if I get some time this week I'll try to recreate the issue.

@jakerella Thanks for replying Jordan. I have tried that, it didn't make any difference.

I think your many-to-many work around probably makes more sense, I'll have a think about how I'd implement that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment