繁体   English   中英

CodeIgniter Datamapper将一对多保存为1:n

[英]CodeIgniter Datamapper save one to many 1:n

我正在使用codeigniter和datamapper来创建一个发票应用程序。

发票has_many Invoice_item

我正在尝试针对发票保存新的Invoice_items。

如果我做以下事情:

$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);
}

这很好但我希望我可以做这样的事情:

$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);

有可能这样做吗? 任何帮助或建议非常感谢,谢谢。

更新:

发票模型

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模型

class Invoice_item extends DataMapper {

  public $has_one = array('invoice');

  public function __construct($id = NULL) {
    parent::__construct($id);
  }

}

如果你使用magick save_{relationname}函数和数组它应该做你想要的,尝试:

$invoice->save_invoice_item($items);

如果将数组传递给save()方法,它将尝试将其中的每个项目解释为关系,并且要使“* -many”工作,您需要在其中包含第二个数组,因此将发票项目包装两次应该执行它也:

$invoice->save(array(array($invoice_item1, $invoice_item2));

不幸的是,当相关对象没有首先保存到数据库时,DM似乎没有处理这种情况(猜测正确的顺序不会是微不足道的)。 所以你必须写它:

$invoice_item1->save();
$invoice_item2->save();
$invoice->save(array(array($invoice_item1, $invoice_item2)));

一旦保存,这将使用发票的ID更新invoice_item行。 这个过程并不理想,因为数据库保留发票项目没有相关发票的时刻,建议使用此处的交易。

您可以通过先保存没有项目的Invoice而不是使用发票实例保存项目来切换订单:

$invoice->save();
$invoice_item = new Invoice_item;
// ...
$invoice_item->save($invoice);

您可以通过从$this->db->queries转储查询来检查发生了什么。

更新

在您的Invoice模型中,您将覆盖原始的save()方法,但是您不会将参数传递给parent::save()因此这样的行如下所示:

$invoice->save($invoice_item); 

只是忽略参数(php不要抱怨更多参数然后需要)。 你可能想写这样的东西:

public function save() {
    // passing arguments the way they came, 
    // it's also more robust against changes in datamapper with func_get_args() + call_user_func()
    call_user_func_array(array('parent', 'save'), func_get_args());

    $this->where('id', $this->id)->update('invoice_no', $this->id);
}

您的原始示例有效,因为您使用Invoice_item类的save()方法进行save()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM