这是一个相当广泛的问题,但我希望我能得到一些指导。
我正在为我的公司建立一个报告系统。我有针对客户、订单、发票和项目的课程。它们适用于单个对象。但是,这是一个报告系统,我需要以多种方式查询和汇总这些对象。
例如,单个 Order 对象将具有该订单的总美元价值。但是,如果我要生成当月的报告,我想总结一组与我传递查询的任何参数(例如日期范围和/或客户编号)相匹配的订单。
这通常涉及额外的工作,例如累积月至今或年至今比较的运行总计。这就是事情对我来说有点模糊的地方。该逻辑是否属于 Order 类?如果没有,那么在哪里?注意我也必须为我的 Invoice 类做同样的事情。
这是我现在使用 Order 类所做的简化版本。我使用一个函数 (getOrders) 返回一个 Order 对象数组,另一个函数 (getOrderGroup) 返回一个分组结果数组(不是对象)。
这是我最不清楚的 getOrdersGroup() 函数。如果有更好的方法来报告分组结果,以及计数、总和和运行总计,请指出更好的路径!
<?php
class Order {
public $number;
public $customer;
public $date_ordered;
public $date_shipped;
public $salesperson;
public $total;
public function __construct(array $data = array()) {
$this->number = $data['number'];
$this->customer = $data['customer'];
$this->date_ordered = $data['date_ordered'];
$this->date_shipped = $data['date_shipped'];
$this->salesperson = $data['salesperson'];
$this->total = $data['total'];
}
/**
* Returns an array of order objects
*/
public static function getOrders(array $options = array()) {
$orders = array();
// Build query to return one or more orders
// $options parameter used to form SQL query
// ......
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$order = new Order($row);
$orders[] = $order;
}
return $orders;
}
/**
* Returns an array of grouped order results (not objects)
*/
public static function getOrdersGroup(array $options = array()) {
$group = array();
// Build query that contains COUNT() and SUM() group by functions
// ......
$running_total = 0;
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// Accumulate running total for the month
$running_total += $row['sum_total'];
// Build the group array with a listing of summarized data
// Note: The order class is never actually instantiated here
// Also, in this example we're grouping by date_ordered...
// but in reality the result should be able to be grouped by a
// dynamic field, such as by customer, or by salesperson,
// or a more detailed breakdown with year, month, day and a running
// total break at each level
$group[] = array(
"date_ordered" => $row["date_ordered"],
"count_customers" => $row["count_customers"],
"count_orders" => $row["count_orders"],
"count_salespersons" => $row["count_salesperson"],
"sum_total" => $row["sum_total"],
"running_total" => $running_total);
}
return $group;
}
/**
* Queries to get ordered items if drilling down to item level...
*/
public function getItems() {
// blah
}
}
?>