I'm using codeigniter and datamapper to create an invoicing app.
an Invoice has_many Invoice_item
I'm trying to save new Invoice_items against an Invoice.
If i do the following:
$invoice = new Invoice(1474);
$invoice_item1 = new Invoice_item();
$invoice_item1->description = 'item 1';
$invoice_item2 = new Invoice_item();
$invoice_item2->description = 'item 2';
$items = array($invoice_item1, $invoice_item2);
foreach ($items as $item) {
$item->save($invoice);
}
This works fine but I was hoping I could do something like this:
$invoice = new Invoice(1474);
$invoice_item1 = new Invoice_item();
$invoice_item1->description = 'item 1';
$invoice_item2 = new Invoice_item();
$invoice_item2->description = 'item 2';
$items = array($invoice_item1, $invoice_item2);
$invoice->save($items);
Is it possible to do it this way? Any help or advice much appreciated, thanks.
Update:
Invoice Model
class Invoice extends DataMapper {
public $has_many = array('invoice_item');
public $has_one = array('customer');
public function __construct($id = NULL) {
parent::__construct($id);
}
public function getTotal() {
$this->invoice_item->get_iterated();
$total = 0;
foreach ($this->invoice_item as $item) {
$total += $item->price * $item->qty;
}
return number_format($total, 2);
}
public function getStatus() {
if (!$this->paid) {
$status = date_diff(date_create(date('Y-m-d')), date_create($this->invoice_date))->format('%a') . " days";
} else {
$status = 'PAID';
}
return $status;
}
public function save() {
parent::save();
$this->where('id', $this->id)->update('invoice_no', $this->id);
}
}
Invoice_item Model
class Invoice_item extends DataMapper {
public $has_one = array('invoice');
public function __construct($id = NULL) {
parent::__construct($id);
}
}