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 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题