I lately started to use Propel (PHP ORM) and I love it but I have one quite annoying issue I can't resolve even with a lot of trying. I use reverse engineering to create my schema.xml, which works great until it comes to joins. Sadly for all my foreign keys the reverse engineering only adds the name
and not the phpName
attribute. Whatever I try to use this name
attribute for a join I fail. After I added the phpName
attribute manually (and then rebuilding the models of course) the join works fine as it should.
Here is the snippet of the foreign key in the schema.xml (without the phpName attribute obviously):
<foreign-key foreignTable="users" name="messages_ibfk_1">
<reference local="creating_user_id" foreign="id"/>
</foreign-key>
And here is the code for my join (which doesn't work):
$messages = MessagesQuery::create()->joinWith('messages_ibfk_1')->findByRecipientId($id);
I tried all kinds of variations for the joinWith
value but none worked. On top of the schema this setting is active: defaultPhpNamingMethod="underscore"
The error propel pulls out is: Unknown relation messages_ibfk_1 on the Messages table.
If I add the phpName
attribut with the value Author
the join works fine like that:
$messages = MessagesQuery::create()->joinWith('Author')->findByRecipientId($id);
As I have a lot of foreign keys and I want minimum/none manual work the question is: How do I resolve this without adding all the phpName
attributes manually. Either I find a way to access the foreign keys with their regular name attribute or there is a way to tell propel to set up the phpName
attribute while building the models?
I hope someone has an idea, would be a great help! :)
foreign-key
s name
attribute is only being used for schema migration, not for the usage in your actual PHP code. So either you define everywhere a useful phpName
or you extend MysqlSchemaParser
for example and add there something like:
$fk->setPhpName($name);
You can then use this new reverse class by using the migration.parserClass
configuration property. More information here http://propelorm.org/documentation/reference/configuration-file.html#reverse-engeneering. Keep in mind: If you change this property, you aren't able to pass a DSN for the database:reverse
command anymore. You need to pass a connection name to it, defined in your propel configuration under propel.database.connections
.
The name messages_ibfk_1
refers to the name of the index in your physical table, and not a relation/table. joinWith() requires the table name, in this case it should be joinWith('users')
, which, according to your schema definition is the foreign table.
So the correct synax should be: $messages = MessagesQuery::create()->joinWith('users')->findByRecipientId($id);