doubi9615 2012-07-05 14:36
浏览 48
已采纳

ManyToOne映射与Doctrine和未定义的索引警告

I'm sure there's a simple solution to this, but I can't fathom what's going on. I have an entity called a Payment and two other entities called User and Household.

A User has many Payments and and a Household has many Payments. This is all mapped as unidirectional manyToOne relationships of the Payment using YAML, like this (hopefully this is in line with the guidance at http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/association-mapping.html#many-to-one-unidirectional) -

Payment:
  type: entity
  table: payments
  id:
    _id:
      type: integer
      column: payment_id
      generator:
        strategy: AUTO
    manyToOne:
    _user:
      targetEntity: Application_Model_User
      joinColumn:
        name: user_id
        referencedColumnName: payment_id
    _household:
      targetEntity: Application_Model_Household
      joinColumn:
        name: household_id
        referencedColumnName: payment_id

Database wise, I have three tables, users, payments and households. The payments table has (in addition to it's primary key) two columns called user_id and household_id, which correspond the to primary keys of the other two tables.

The problem is that when I try to persist a new Payment I get an error back from mySql telling me that the user_id column of payments cannot be null. Some digging has established that doctrine is supplying null for both user_id and household_id in the INSERT statement. The object graph is definitely correct.

In addition, I get the following warnings:

Notice: Undefined index: payment_id in [..]/library/Doctrine/ORM/Persisters/BasicEntityPersister.php on line 511

Notice: Undefined index: in [..]/library/Doctrine/ORM/Persisters/BasicEntityPersister.php on line 511

Notice: Undefined index: payment_id in [..]/library/Doctrine/ORM/Persisters/BasicEntityPersister.php on line 511

Notice: Undefined index: in [..]/library/Doctrine/ORM/Persisters/BasicEntityPersister.php on line 511

I've established that the payment_id in question is coming from the referencedColumnNames in my mapping, and that the index is undefined because the array doctrine is searching contains the fields from the opposite side of the relationship, i.e. the Household or the User. But surely it shouldn't be? I think I've followed the guide, but I can't make it work.

I'm using Zend Framework, if it helps, but there's nothing special going on my code. I'm pretty sure it's a mapping problem.

Thanks

  • 写回答

1条回答 默认 最新

  • douhezi2285 2012-07-05 14:57
    关注

    I've only worked with Doctrine + Symfony2, but I think your referencedColumnName should just be "id", not payment_id, because you need to explain the foreign key relationship.

    payment.household_id --references--> household.id.

    In Symfony2 there is an 'inversedBy' property for Doctrine, which is used to map the column in the other table that would represent the payment_id.

    A snippet of code from a Symfony2 Mapping:

    /**
    * @ORM\ManyToOne(targetEntity="kurtfunai\WalkthroughBundle\Entity\Group", inversedBy="members")
    * @ORM\JoinColumn(name="fk_group_id", referencedColumnName="id")
    */
    protected $group;
    

    In your case it would need altered to something like this:

    _user:
      targetEntity: Application_Model_User
      joinColumn:
        name: user_id
        referencedColumnName: <USERS_TABLE_PRIMARY_KEY_COLUMN_NAME>
    _household:
      targetEntity: Application_Model_Household
      joinColumn:
        name: household_id
        referencedColumnName: <HOUSEHOLDS_TABLE_PRIMARY_KEY_COLUMN_NAME>
    

    (Replace with the appropriate column names)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么