0

In a Doctrine 1.2 project we've got a User

User:
  columns:
    id:
      type: integer(4)
      autoincrement: true
      primary: true
    payment_method:
      type: integer(4)
  relations:
    DefaultPaymentMethod:
      class: PaymentMethod
      local: payment_method
      foreignAlias: Users
      foreign: id
      type: one

with his PaymentMethods

PaymentMethod:
  actAs:
    BS_Db_Template_SoftDelete:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    user:
      type: integer(4)
  relations:
    User:
      local: user
      foreign: id
      foreignAlias: PaymentMethods
      type: one

If I try setting one of his payment method and his default payment method...

$user = new User();

$paymentMethod = new PaymentMethod();
$paymentMethod->set('User',$user);

$user->set('DefaultPaymentMethod',$paymentMethod);

$user->save();

...this line

$user->set('DefaultPaymentMethod',$paymentMethod);

...deletes his payment method (so User of PaymentMethod isn't set), when I try to save it.

Am I doing something wrong (logical issue) or is this a Doctrine bug?

1 Answers1

0

Why did you define the payment method twice?

Your foreign key to PaymentMethod is payment_method. So, if you define it once, it's ok.

You can try that:

$user = new User();

$paymentMethod = new PaymentMethod();
$paymentMethod->setUser($user);
$paymentMethod->save();

$user->setDefaultPaymentMethod($paymentMethod);
$user->save();

set method use the relation name to define the relation.

j0k
  • 22,600
  • 28
  • 79
  • 90
  • I tried to save a whole object tree at a single blow, therefore I appended the payment method to the User object with $user->PaymentMethods[] = $paymentMethod; It works with all other references (there are also Catalogs, Objects, Bases, Productions) in this tree. – Andreas Bernhard Jan 30 '13 at 08:52
  • But you shouldn't do both, ie: `$user->PaymentMethods[] = $paymentMethod;` and this `$user->DefaultPaymentMethod = $paymentMethod;`. Choose one of them only. I recommend the second one. – j0k Jan 30 '13 at 08:55
  • Thank you for your effort. I implemted it like you recommended and the reference from User to PaymentMethod is set like expected. But PaymentMethod->User isn't set. – Andreas Bernhard Jan 30 '13 at 09:07
  • Did you save it after the setting? Could you update your code? – j0k Jan 30 '13 at 09:55
  • Updated my code above, I want to save the whole tree all at once for transaction security. – Andreas Bernhard Jan 30 '13 at 10:44
  • Did you try to `$paymentMethod->save();` after `$paymentMethod->set('User',$user);` ? – j0k Jan 30 '13 at 11:11
  • Yes, then it works. But this can't be a solution for a ORM, right? – Andreas Bernhard Jan 30 '13 at 11:15
  • Oh I see, you want to perform only one save and *save them all*? Hum, that should be done.. – j0k Jan 30 '13 at 12:26
  • Have it solved by splitting it up into two save()'s. I think the double dependency was the problem. PaymentMethod has a User and User as a PaymentMethod. In the MySQL query log PaymentMethod was inserted first and then the User was inserted and PaymentMethod not updated. Many thanks for your effort anyway! – Andreas Bernhard Jan 31 '13 at 08:51