mysql - PHP Best Practices for querying a group of class objects with summarized data -


this pretty broad question i'm hoping bit of guidance.

i'm building reporting system company. have classes customer, order, invoice, , item. work great individual objects. however, reporting system , need query , summarize these objects in variety of ways.

for example, single order object have total dollar value 1 order. if i'm generating report month, want summarize group of orders match whatever parameters pass query (such date range and/or customer number).

this typically involves additional work such accumulating running totals month date or year date comparisons. things little fuzzy me. logic belong in order class? if not, where? note have same invoice class.

here's simplified version of i'm doing order class. use 1 function (getorders) returns array of order objects, , function (getordergroup) returns array of grouped results (not objects).

it's getordersgroup() function i'm unclear about. if there better practice reporting on grouped results, along counts, sums , running totals, please point me down better path!

<?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 array of order objects      */              public static function getorders(array $options = array()) {         $orders = array();          // build query return 1 or more orders         // $options parameter used form sql query         // ......          $result = mysql_query($query);         while ($row = mysql_fetch_array($result, mysql_assoc)) {             $order = new order($row);             $orders[] = $order;         }         return $orders;     }      /**      * returns array of grouped order results (not objects)      */              public static function getordersgroup(array $options = array()) {         $group = array();          // build query contains count() , sum() group functions         // ......          $running_total = 0;         $result = mysql_query($query);         while ($row = mysql_fetch_array($result, mysql_assoc)) {              // accumulate running total month             $running_total += $row['sum_total'];              // build group array listing of summarized data             // note: order class never instantiated here             // also, in example we're grouping date_ordered...             // in reality result should able grouped             // dynamic field, such customer, or salesperson,             // or more detailed breakdown year, month, day , running             // total break @ 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 ordered items if drilling down item level...      */                  public function getitems() {         // blah     } } ?> 

there number of tasks done and, yes, of them specific each class. when comes knowing columns there , how treat them. on other hand there abstract logic of 'grouping something' , of building search patterns (and includes of course range of date handling functions). these 2 can/should placed in separate objects/functions can called both classes, orders , invoices.

a similar thing goes putting result arrays. here need able adapt sorts of different grouping conditions. column names class specific general logic can put in separate classes or functions.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -