<?php
class Order extends CActiveRecord {
...
protected function afterSave() { // invoked after the order instance is saved
parent::afterSave();
foreach($this->order_lines as $order_line) {
$order_line->order_id = $this->id; // ensure to set the order id on each order line
$order_line->save();
}
}
}
then of course it’s a good idea to encapsulate your $order->save() in a transaction if you are using a transactional database engine in this case - it’s easy to forget about transactions when wrapping everything up like this
My relation is called ‘order_lines’ though and therefore, I probably have to create a separate variable for this. If I use the same name as the relation I’m getting a ‘Modification of overloaded property has no effect’ warning
I guess this is because CActiveRecord’s __get() function doesn’t return by reference.