doufeng2877 2015-05-03 07:37
浏览 42
已采纳

使用Propel联接的名称属性

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! :)

  • 写回答

2条回答 默认 最新

  • dslpofp041310584 2015-05-08 12:17
    关注

    foreign-keys 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);
    

    https://github.com/propelorm/Propel2/blob/master/src/Propel/Generator/Reverse/MysqlSchemaParser.php#L349

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容