Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most correct way around this propel "Cannot redeclare function" error?

I have the following tables:

user, logbook, user_logbook

User logbook is a junction table (isCrossRef="true" in the schema.xml), containing user_id and logbook_id foreign keys.

I also wanted to remember the currently selected logbook for any given user, so I put a current_logbook_id in my user table also, and gave that a foreign key. However, in my BaseUserQuery.php file I get two filterByLogbook() functions.

I know that when you have two foreign keys to the same table, from the same table, you get functions like getUserRelatedBySomething() and getUserRelatedBySomethingElse(), but what is the best thing to do in this situation?

I can of course just remove the foreign key and just use the ID saved, or I suppose I could create a new junction table (though that doesn't seem correct). What is the best thing to do in this situation, in terms of "doing the right thing" in MySQL and Propel (hopefully I'll be able to do both!).

like image 459
LeonardChallis Avatar asked Nov 03 '22 16:11

LeonardChallis


1 Answers

In your schema.xml add phpName and refPhpName attributes to the foreign key in the user table definition:

  <table name="user" phpName="User">
    ...
    <foreign-key foreignTable="logbook" phpName="CurrentLogBook" refPhpName="CurrentLogBookUser">
      <reference local="current_logbook_id" foreign="id"/>
    </foreign-key>
    ...
  </table>

Then you should get filterByCurrentLogBook() and filterByCurrentLogBookUser() functions and no collisions.

like image 61
Rob Agar Avatar answered Nov 08 '22 05:11

Rob Agar