简体   繁体   English

CodeIgniter DataMapper相关项目的顺序

[英]CodeIgniter DataMapper order of related items

I'm writing an invoicing application in CodeIgniter/DataMapper. 我正在CodeIgniter / DataMapper中编写发票应用程序。

Each customer has many invoices. 每个客户都有很多发票。

In the view, I want to be able to display customer data and then a table of the invoices for that customer. 在该视图中,我希望能够显示客户数据,然后显示该客户的发票表。

At the moment, I load the customer and their related invoices in the controller and send them to the view. 目前,我将客户及其相关发票加载到控制器中,并将其发送到视图。

public function view($id) {
    $c = new Customer($id);
    $c->invoice->get_iterated();
    $data['customer'] = $c;
}

But when I iterate over the invoices to display them in the view, I really want them to be in descending order by date, but of course they're not. 但是,当我遍历发票以在视图中显示它们时,我真的希望它们按日期降序排列,但是当然不是。

Is there any way to specify the order of related items or to sort them before displaying them, or do I need to load the invoices separately, something like in the following the controller? 有什么方法可以指定相关项目的顺序或在显示它们之前对它们进行排序,还是我需要分别加载发票,例如以下控制器中所示?

public function view($id) {
    $c = new Customer($id);
    $i = new Invoice();
    $i->order_by('invoice_date', 'desc');
    $i->get_where(array('customer_id' => $id));
    $data['customer'] = $c;
    $data['invoices'] = $i;
}

You can simply add an ->order_by() call before issuing the get_iterated() . 您可以在发出get_iterated()之前简单地添加->order_by()调用。 So in your examples it would look something like: 因此,在您的示例中,它看起来像:

$c->invoice->order_by('invoice_date', 'desc')->get_iterated();

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

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