From: Iain Patterson Date: Thu, 7 Apr 2016 15:30:24 +0000 (-0400) Subject: More efficient order querying when mask is set. X-Git-Tag: 2016-04-07~1 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=5f06d69fa871be3fceb974753134db509eb0d80a;p=readifood.git More efficient order querying when mask is set. Use a join against the area and city instead of first finding all contacts in the desired location. --- diff --git a/lib/order.php b/lib/order.php index 3e3556f..2476ef1 100644 --- a/lib/order.php +++ b/lib/order.php @@ -42,7 +42,7 @@ exit; } - function show_orders($requester_ids = null, $beneficiary_ids = null, $state_mask = null) { + function show_orders($city_id = null, $area_id = null, $requester_ids = null, $beneficiary_ids = null, $state_mask = null) { list($first_page, $per_page) = pagination(); /* XXX: Use Propel methods. */ if (isset($state_mask)) $order_ids = get_order_ids_by_state($state_mask); @@ -51,6 +51,15 @@ if (isset($beneficiary_ids)) $q->filterByBeneficiaryId($beneficiary_ids); # XXX: Doesn't work. #if (isset($state_mask)) $q->useOrderStateQuery()->addSelectQuery($latest_state, 'latestState')->where("order_id=latestState.order_id")->where("state & $state_mask")->endUse(); + if (isset($city_id) || isset($area_id)) { + $q->joinWith('Order.Beneficiary'); + $q->joinWith('Beneficiary.Address'); + if (isset($area_id)) $q->where('Address.AreaId=?', $area_id); + if (isset($city_id)) { + $q->joinWith('Address.Area'); + $q->where('Area.CityId=?', $city_id); + } + } if (isset($state_mask)) $q->filterById($order_ids); $orders = $q->orderByDate('desc')->orderById('desc')->paginate($first_page, $per_page); if (count($orders)) { @@ -66,12 +75,8 @@ if (isset($city_id)) $city = get_city_by_id($city_id); else if ($city_name) $city = get_city_by_name($city_name); if ($city) { - $contacts = get_city_contacts($city->getId(), $GLOBALS['ROLE_BENEFICIARY']); - $beneficiary_ids = array(); - foreach ($contacts as $contact) $beneficiary_ids[] = $contact->getId(); - echo "

Orders in city " . $city->getLink(get_city_displayname($city)) . ":"; - return show_orders(null, $beneficiary_ids, $state_mask); + return show_orders($city->getId(), null, null, null, $state_mask); } else echo "

No such city!

\n"; } @@ -81,7 +86,7 @@ else if ($contact_name) $contact = get_contact_by_name($contact_name); if ($contact) { echo "

Orders from referrer " . $contact->getLink() . ":"; - return show_orders($contact->getId(), null, $state_mask); + return show_orders(null, null, $contact->getId(), null, $state_mask); } else echo "

No such contact!

\n"; } @@ -91,7 +96,7 @@ else if ($contact_name) $contact = get_contact_by_name($contact_name); if ($contact) { echo "

Orders to beneficiary " . $contact->getLink() . ":"; - return show_orders(null, $contact->getId(), $state_mask); + return show_orders(null, null, null, $contact->getId(), $state_mask); } else echo "

No such contact!

\n"; } @@ -100,12 +105,8 @@ if (isset($area_id)) $area = get_area_by_id($area_id); else if ($area_name) $area = get_area_by_name($area_name); if ($area) { - $contacts = get_area_contacts($area->getId(), $GLOBALS['ROLE_BENEFICIARY']); - $contact_ids = array(); - foreach ($contacts as $contact) $contact_ids[] = $contact->getId(); - echo "

Orders in area " . $area->getLink() . ":"; - return show_orders(null, $contact_ids, $state_mask); + return show_orders(null, $area->getId(), null, null, $state_mask); } else echo "

No such area!

\n"; } @@ -694,7 +695,7 @@ } } else if (isset($id)) show_order($id); - else if ($state_mask) show_orders(null, null, $state_mask); + else if ($state_mask) show_orders(null, null, null, null, $state_mask); show_order_forms($city_id, $state_mask); show_add_new_order_form($city_id);