[DDC-1269] Unexpected behavior while using association on a non primary key field Created: 11/Jul/11  Updated: 01/Apr/14  Resolved: 01/Apr/14

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: None
Affects Version/s: 2.1
Fix Version/s: None
Security Level: All

Type: Improvement Priority: Major
Reporter: Alexandr Torchenko Assignee: Benjamin Eberlei
Resolution: Invalid Votes: 0
Labels: None

Issue Links:
Reference
is referenced by DDC-1114 Association on a non primary key fiel... Closed

 Description   

We have association on non primary key. Something like this:

Entities\Payment:
  type: entity
  table: payments
  fields:
    id: 
      id: true
      type: integer
      nullable: false
      generator:
        strategy: IDENTITY
[-- skipped --]
  manyToOne:
    order:
      targetEntity: Entities\Order
      inversedBy: payments
      joinColumn:
        name: scode
        referencedColumnName: scode
Entities\Order:
  type: entity
  table: h_orders
  fields:
    id:
      id: true
      type: integer
      unsigned: false
      nullable: false
      generator:
        strategy: IDENTITY
    scode:
      type: integer
      unsigned: false
      nullable: false
[-- skipped --]
  oneToMany:
    payments:
      targetEntity: Entities\Payment
      mappedBy: order

When I try to fetch Order from Payment with lazy loading I receive empty Order object with null properties. If I use eager fetching Order object is valid.
SQL generated for lazy loading seems to be valid, so I suppose the problem is in mapping result to the object. At the same time lazy loading works fine with 2.0.6 version.

Another problem appears while persisting new Payment.

$payment = new \Entities\Payment();
...
$order = $this->em->getRepository('\Entities\Order')->find(46320);
$payment->setOrder($order);
$order->addPayments($payment);
$this->em->persist($payment);
$this->em->flush();

I get this error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'scode' cannot be null' in /usr/share/php/Doctrine/DBAL/Statement.php:131

I found issue which is still open and looks like mine – http://www.doctrine-project.org/jira/browse/DDC-1114. What do you think about this?



 Comments   
Comment by Benjamin Eberlei [ 12/Jul/11 ]

Formatting, please add a second ticket for the second issue.

Comment by Benjamin Eberlei [ 12/Jul/11 ]

I don't think its supported to use a non primary id for foreign key matching. I cant tell for sure though since i wasnt responsible to design this part of the Doctrine code. I would strongly suggest not to do this.

Comment by Benjamin Eberlei [ 12/Jul/11 ]

Marked as improvement. The problem is we cannot detect this invalid mapping, so no exception is thrown during compilation of the mappings,

Comment by Benjamin Eberlei [ 12/Jul/11 ]

This kind of mapping error is already acknowledged by the schema-validator console task.

Comment by Alexandr Torchenko [ 13/Jul/11 ]

Should I create second ticket?

Please confirm that I understood correctly. Should we avoid such mapping as it is considered as invalid.

Comment by Benjamin Eberlei [ 13/Jul/11 ]

Yes, it will not work at all. You dont need to create the second ticket as that error steams from the mapping error.

You will see an error message when calling ./doctrine orm:schema:validate with this mapping.

Comment by Steve Müller [ 01/Apr/14 ]

This usage is not supported. See: http://www.doctrine-project.org/jira/browse/DDC-1114

Generated at Sat Oct 25 08:06:47 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.