From 5259d4e64e79ad160a81ab51e3ef73761a30d2eb Mon Sep 17 00:00:00 2001 From: rMalie Date: Fri, 2 Dec 2011 15:20:47 +0000 Subject: [PATCH] // Use Collection instead of HydrateCollection in several places --- classes/Cart.php | 12 ++++++---- classes/CartRule.php | 22 ++++++++--------- classes/Collection.php | 28 +++++++++++++++++++--- classes/PaymentModule.php | 4 ++-- classes/ProductSupplier.php | 23 +++++++----------- classes/order/Order.php | 20 +++++++--------- classes/order/OrderInvoice.php | 6 +++-- classes/order/OrderPayment.php | 6 +++-- classes/stock/Stock.php | 6 +++-- classes/stock/StockManager.php | 17 +++++-------- classes/stock/SupplyOrder.php | 19 ++++----------- classes/stock/SupplyOrderDetail.php | 13 ++++------ classes/stock/WarehouseProductLocation.php | 20 +++++++--------- 13 files changed, 97 insertions(+), 99 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index 9ba6d7baa..c99cdd588 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -312,15 +312,17 @@ class CartCore extends ObjectModel foreach ($result as &$row) { - $cart_rule = new CartRule($row['id_cart_rule'], (int)$this->id_lang); - $row['value_real'] = $cart_rule->getContextualValue(true); - $row['value_tax_exc'] = $cart_rule->getContextualValue(false); + $row['obj'] = new CartRule($row['id_cart_rule'], (int)$this->id_lang); + $row['value_real'] = $row['obj']->getContextualValue(true); + $row['value_tax_exc'] = $row['obj']->getContextualValue(false); // Retro compatibility < 1.5.0.2 $row['id_discount'] = $row['id_cart_rule']; $row['description'] = $row['name']; } + $results = $this->getCartRule(); + return $result; } @@ -1305,8 +1307,8 @@ class CartCore extends ObjectModel if ($type != Cart::ONLY_PRODUCTS && CartRule::isFeatureActive()) { $result = $this->getCartRules(); - foreach (ObjectModel::hydrateCollection('CartRule', $result, Configuration::get('PS_LANG_DEFAULT')) as $cart_rule) - $order_total_discount += Tools::ps_round($cart_rule->getContextualValue($with_taxes)); + foreach ($result as $row) + $order_total_discount += Tools::ps_round($row['obj']->getContextualValue($with_taxes)); $order_total_discount = min(Tools::ps_round($order_total_discount), $wrapping_fees + $order_total_products + $shipping_fees); $order_total -= $order_total_discount; diff --git a/classes/CartRule.php b/classes/CartRule.php index e537937ba..e4f8d5be2 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -57,8 +57,10 @@ class CartRuleCore extends ObjectModel public $date_add; public $date_upd; - protected $table = 'cart_rule'; - protected $identifier = 'id_cart_rule'; + public static $definition = array( + 'table' => 'cart_rule', + 'identifier' => 'id_cart_rule', + ); protected $fieldsRequired = array('date_from', 'date_to'); protected $fieldsSize = array('code' => 254, 'description' => 65534); @@ -244,10 +246,10 @@ class CartRuleCore extends ObjectModel public static function deleteByIdCustomer($id_customer) { $return = true; - $result = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'cart_rule` WHERE `id_customer` = '.(int)$id_customer); - $cartRules = ObjectModel::hydrateCollection('CartRule', $result); - foreach ($cartRules as $cartRule) - $return &= $cartRule->delete(); + $cart_rules = new Collection('CartRule'); + $cart_rules->where('a.id_customer = '.(int)$id_customer); + foreach ($cart_rules as $cart_rule) + $return &= $cart_rule->delete(); return $return; } @@ -636,13 +638,11 @@ class CartRuleCore extends ObjectModel $errors = array(); if (!$context) $context = Context::getContext(); - $result = $context->cart->getCartRules(); - $cartRules = ObjectModel::hydrateCollection('CartRule', $result); - foreach ($cartRules as $cartRule) + foreach ($context->cart->getCartRules() as $cart_rule) { - if ($error = $cartRule->checkValidity($context, true)) + if ($error = $cart_rule['obj']->checkValidity($context, true)) { - $context->cart->removeCartRule($cartRule->id); + $context->cart->removeCartRule($cart_rule['obj']->id); $context->cart->update(); $errors[] = $error; } diff --git a/classes/Collection.php b/classes/Collection.php index c6154bd91..50a63fe5f 100644 --- a/classes/Collection.php +++ b/classes/Collection.php @@ -30,7 +30,7 @@ * * @since 1.5.0 */ -class CollectionCore implements Iterator +class CollectionCore implements Iterator, Countable { /** * @var string Object class name @@ -62,6 +62,11 @@ class CollectionCore implements Iterator */ protected $iterator = 0; + /** + * @var bool Is current collection already hydrated + */ + protected $is_hydrated = false; + /** * @param string $classname * @param int $id_lang @@ -79,7 +84,7 @@ class CollectionCore implements Iterator $this->query->select('a.*'); $this->query->from($this->definition['table'].' a'); - // If multilang, create assciation to lang table + // If multilang, create association to lang table if (isset($this->definition['multilang']) && $this->definition['multilang']) { $this->query->select('b.*'); @@ -108,6 +113,7 @@ class CollectionCore implements Iterator public function orderBy($str) { $this->query->orderBy($str); + return $this; } /** @@ -119,6 +125,7 @@ class CollectionCore implements Iterator public function groupBy($str) { $this->query->groupBy($str); + return $this; } /** @@ -129,10 +136,14 @@ class CollectionCore implements Iterator */ public function getAll($display_query = false) { + if ($this->is_hydrated) + return $this; + $this->is_hydrated = true; + if ($display_query) echo $this->query.'
'; - $this->results = Db::getInstance()->executeS($this->query); + $this->results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($this->query); $this->results = ObjectModel::hydrateCollection($this->classname, $this->results, $this->id_lang); return $this; @@ -178,4 +189,15 @@ class CollectionCore implements Iterator { $this->iterator++; } + + /** + * Get total of results + * + * @return int + */ + public function count() + { + $this->getAll(); + return count($this->results); + } } \ No newline at end of file diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index f184a29a2..3dc537858 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -296,9 +296,9 @@ abstract class PaymentModuleCore extends Module $cartRulesList = ''; $result = $cart->getCartRules(); - $cartRules = ObjectModel::hydrateCollection('CartRule', $result, (int)$order->id_lang); - foreach ($cartRules as $cartRule) + foreach ($result as $cart_rule) { + $cartRule = $cart_rule['obj']; $value = $cartRule->getContextualValue(true); // Todo : has not been tested because order processing wasn't functionnal if ($value > $order->total_products_wt && $cartRule->partial_use == 1 && $cartRule->reduction_amount > 0) diff --git a/classes/ProductSupplier.php b/classes/ProductSupplier.php index dcd89c429..41c571b60 100644 --- a/classes/ProductSupplier.php +++ b/classes/ProductSupplier.php @@ -70,8 +70,10 @@ class ProductSupplierCore extends ObjectModel 'id_currency' => 'isUnsignedId', ); - protected $table = 'product_supplier'; - protected $identifier = 'id_product_supplier'; + public static $definition = array( + 'table' => 'product_supplier', + 'primary' => 'id_product_supplier', + ); public function getFields() { @@ -158,22 +160,15 @@ class ProductSupplierCore extends ObjectModel * * @param int $id_product * @param int $group_by_supplier - * @return array + * @return Collection */ public static function getSupplierCollection($id_product, $group_by_supplier = true) { - // build query - $query = new DbQuery(); - $query->select('*'); - $query->from('product_supplier ps'); - $query->where('ps.id_product = '.(int)$id_product); + $suppliers = new Collection('ProductSupplier'); + $suppliers->where('a.id_product = '.(int)$id_product); + $suppliers->groupBy('a.id_supplier'); - if ($group_by_supplier) - $query->groupBy('ps.id_supplier'); - - $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); - - return ObjectModel::hydrateCollection('ProductSupplier', $results); + return $suppliers; } public function delete() diff --git a/classes/order/Order.php b/classes/order/Order.php index 5e7d42500..195845380 100644 --- a/classes/order/Order.php +++ b/classes/order/Order.php @@ -1316,15 +1316,13 @@ class OrderCore extends ObjectModel /** * This method allows to get all Order Payment for the current order * @since 1.5.0.1 - * @return array Collection of Order Payment + * @return Collection of Order Payment */ public function getOrderPaymentCollection() { - $order_payment = Db::getInstance()->ExecuteS(' - SELECT * - FROM `'._DB_PREFIX_.'order_payment` - WHERE `id_order` = '.(int)$this->id); - return ObjectModel::hydrateCollection('OrderPayment', $order_payment); + $order_payments = new Collection('OrderPayment'); + $order_payments->where('id_order = '.(int)$this->id); + return $order_payments; } /** @@ -1402,15 +1400,13 @@ class OrderCore extends ObjectModel * * Get all invoices for the current order * @since 1.5.0.1 - * @return array Collection of Order invoice + * @return Collection of Order invoice */ public function getInvoicesCollection() { - $invoices = Db::getInstance()->ExecuteS(' - SELECT * - FROM `'._DB_PREFIX_.'order_invoice` - WHERE `id_order` = '.(int)$this->id); - return ObjectModel::hydrateCollection('OrderInvoice', $invoices); + $order_invoices = new Collection('OrderInvoice'); + $order_invoices->where('id_order = '.(int)$this->id); + return $order_invoices; } /** diff --git a/classes/order/OrderInvoice.php b/classes/order/OrderInvoice.php index c19adcfbd..70f7fa399 100644 --- a/classes/order/OrderInvoice.php +++ b/classes/order/OrderInvoice.php @@ -69,8 +69,10 @@ class OrderInvoiceCore extends ObjectModel protected $fieldsRequired = array('id_order', 'number'); protected $fieldsValidate = array('id_order' => 'isUnsignedId', 'number' => 'isUnsignedId'); - protected $table = 'order_invoice'; - protected $identifier = 'id_order_invoice'; + public static $definition = array( + 'table' => 'order_invoice', + 'identifier' => 'id_order_invoice', + ); public function getFields() { diff --git a/classes/order/OrderPayment.php b/classes/order/OrderPayment.php index 5a887feb5..34ede321f 100644 --- a/classes/order/OrderPayment.php +++ b/classes/order/OrderPayment.php @@ -54,8 +54,10 @@ class OrderPaymentCore extends ObjectModel 'card_holder' => 'isAnything' ); - protected $table = 'order_payment'; - protected $identifier = 'id_order_payment'; + public static $definition = array( + 'table' => 'order_payment', + 'primary' => 'id_order_payment', + ); public function getFields() { diff --git a/classes/stock/Stock.php b/classes/stock/Stock.php index 98a63b9c7..9b3a8dabe 100644 --- a/classes/stock/Stock.php +++ b/classes/stock/Stock.php @@ -82,8 +82,10 @@ class StockCore extends ObjectModel 'price_te' => 'isPrice', ); - protected $table = 'stock'; - protected $identifier = 'id_stock'; + public static $definition = array( + 'table' => 'stock', + 'identifier' => 'id_stock', + ); public function getFields() { diff --git a/classes/stock/StockManager.php b/classes/stock/StockManager.php index 6fc621a96..979abb5fd 100644 --- a/classes/stock/StockManager.php +++ b/classes/stock/StockManager.php @@ -612,22 +612,17 @@ class StockManagerCore implements StockManagerInterface * * @param int $id_product * @param int $id_product_attribute - * @return array + * @return Collection */ protected function getStockCollection($id_product, $id_product_attribute, $id_warehouse = null, $price_te = null) { - // build query - $query = new DbQuery(); - $query->select('s.id_stock, s.physical_quantity, s.usable_quantity, s.price_te, s.id_product, s.id_product_attribute, s.id_warehouse'); - $query->from('stock s'); - $query->where('s.id_product = '.(int)$id_product.' AND s.id_product_attribute = '.(int)$id_product_attribute); + $stocks = new Collection('Stock'); + $stocks->where('a.id_product = '.(int)$id_product.' AND a.id_product_attribute = '.(int)$id_product_attribute); if ($id_warehouse) - $query->where('s.id_warehouse = '.(int)$id_warehouse); + $stocks->where('a.id_warehouse = '.(int)$id_warehouse); if ($price_te) - $query->where('s.price_te = '.(float)$price_te); + $stocks->where('a.price_te = '.(float)$price_te); - $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); - - return ObjectModel::hydrateCollection('Stock', $results); + return $stocks; } } \ No newline at end of file diff --git a/classes/stock/SupplyOrder.php b/classes/stock/SupplyOrder.php index a953e4d73..74e1bca86 100755 --- a/classes/stock/SupplyOrder.php +++ b/classes/stock/SupplyOrder.php @@ -297,22 +297,13 @@ class SupplyOrderCore extends ObjectModel /** * Retrieves the product entries collection for the current order * - * @return array + * @return Collection */ - public function getEntriesCollection($id_lang = null) + public function getEntriesCollection() { - if ($id_lang == null) - $id_lang = Context::getContext()->language->id; - - // build query - $query = new DbQuery(); - $query->select('s.*'); - $query->from('supply_order_detail s'); - $query->where('s.id_supply_order = '.(int)$this->id); - - $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); - - return ObjectModel::hydrateCollection('SupplyOrderDetail', $results); + $details = new Collection('SupplyOrderDetail'); + $details->where('a.id_supply_order = '.(int)$this->id); + return $details; } diff --git a/classes/stock/SupplyOrderDetail.php b/classes/stock/SupplyOrderDetail.php index e09947e3a..66fe827f5 100755 --- a/classes/stock/SupplyOrderDetail.php +++ b/classes/stock/SupplyOrderDetail.php @@ -187,15 +187,10 @@ class SupplyOrderDetailCore extends ObjectModel 'price_with_order_discount_te' => 'isPrice', ); - /** - * @var string Database table name - */ - protected $table = 'supply_order_detail'; - - /** - * @var string Database ID name - */ - protected $identifier = 'id_supply_order_detail'; + public static $definition = array( + 'table' => 'supply_order_detail', + 'primary' => 'id_supply_order_detail', + ); /** * @see ObjectModel::getFields() diff --git a/classes/stock/WarehouseProductLocation.php b/classes/stock/WarehouseProductLocation.php index aeeb500c6..b9080e652 100644 --- a/classes/stock/WarehouseProductLocation.php +++ b/classes/stock/WarehouseProductLocation.php @@ -58,8 +58,10 @@ class WarehouseProductLocationCore extends ObjectModel 'id_warehouse' => 'isUnsignedId', ); - protected $table = 'warehouse_product_location'; - protected $identifier = 'id_warehouse_product_location'; + public static $definition = array( + 'table' => 'warehouse_product_location', + 'primary' => 'id_warehouse_product_location', + ); public function getFields() { @@ -122,18 +124,12 @@ class WarehouseProductLocationCore extends ObjectModel * * @param int $id_product * @param int $id_lang - * @return array + * @return Collection */ public static function getCollection($id_product) { - // build query - $query = new DbQuery(); - $query->select('*'); - $query->from('warehouse_product_location wpl'); - $query->where('wpl.id_product = '.(int)$id_product); - - $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); - - return ObjectModel::hydrateCollection('WarehouseProductLocation', $results); + $collection = new Collection('WarehouseProductLocation'); + $collection->where('a.id_product = '.(int)$id_product); + return $collection; } } \ No newline at end of file