2

I set up an many to many relationship between Orders and Sets. An Order can contain many Sets and different Sets can belong to different Orders. Because you can set the amount for a set in a order there should be an additional column for amount. So e.g. an Order can consist of 5 x "Set A" and 10 x "Set B".

This is the schema of the join table:

OrderSet:columns:
amount: integer
order_id:
  type: integer
  primary: true
set_id:
  type: integer
  primary: true

Works fine so far, I just don“t know how I can set the value of the amount column.

This is how I save the order / set-order relationship:

public function saveOrder($data){
    $tempSets = $data->sets;
    $order = new Order();
    unset($data->sets);
    $order->merge((array) $data);

    foreach($tempSets as $set){
        $q = Doctrine_Query::create()
        ->from('Set s')
        ->where('s.id = ?', $set->id);
        $set = $q->fetchOne();
        $order->sets->add($set);
    }
    $order->save();
}

How can I set the amount of each set?

Many to many thanx for your help. cheers, Florian

spierala
  • 2,349
  • 3
  • 25
  • 50

1 Answers1

1

ah i found this post: NHibernate: Many-to-many relationship with field in the relationship table

they say... treat the relation table as entity.

now thats the way it works:

public function saveOrder($data){
    $tempSets = $data->sets;
    $order = new Order();
    unset($data->sets);
    $order->merge((array) $data);
    $order->save(); //save order first to make the order id available

    foreach($tempSets as $set){         
        $orderSet = new OrderSet(); //create an object of the Class representing the relation table
        $orderSet->order_id = $order->id;
        $orderSet->set_id = $set->id;
        $orderSet->amount = $set->amount;
        $orderSet->save();
    }
}

hope that can help someone else too.

cheers, florian

Community
  • 1
  • 1
spierala
  • 2,349
  • 3
  • 25
  • 50