Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top