// Use Collection instead of HydrateCollection in several places

This commit is contained in:
rMalie
2011-12-02 15:20:47 +00:00
parent fec7ede55c
commit 5259d4e64e
13 changed files with 97 additions and 99 deletions
+7 -5
View File
@@ -312,15 +312,17 @@ class CartCore extends ObjectModel
foreach ($result as &$row) foreach ($result as &$row)
{ {
$cart_rule = new CartRule($row['id_cart_rule'], (int)$this->id_lang); $row['obj'] = new CartRule($row['id_cart_rule'], (int)$this->id_lang);
$row['value_real'] = $cart_rule->getContextualValue(true); $row['value_real'] = $row['obj']->getContextualValue(true);
$row['value_tax_exc'] = $cart_rule->getContextualValue(false); $row['value_tax_exc'] = $row['obj']->getContextualValue(false);
// Retro compatibility < 1.5.0.2 // Retro compatibility < 1.5.0.2
$row['id_discount'] = $row['id_cart_rule']; $row['id_discount'] = $row['id_cart_rule'];
$row['description'] = $row['name']; $row['description'] = $row['name'];
} }
$results = $this->getCartRule();
return $result; return $result;
} }
@@ -1305,8 +1307,8 @@ class CartCore extends ObjectModel
if ($type != Cart::ONLY_PRODUCTS && CartRule::isFeatureActive()) if ($type != Cart::ONLY_PRODUCTS && CartRule::isFeatureActive())
{ {
$result = $this->getCartRules(); $result = $this->getCartRules();
foreach (ObjectModel::hydrateCollection('CartRule', $result, Configuration::get('PS_LANG_DEFAULT')) as $cart_rule) foreach ($result as $row)
$order_total_discount += Tools::ps_round($cart_rule->getContextualValue($with_taxes)); $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_discount = min(Tools::ps_round($order_total_discount), $wrapping_fees + $order_total_products + $shipping_fees);
$order_total -= $order_total_discount; $order_total -= $order_total_discount;
+11 -11
View File
@@ -57,8 +57,10 @@ class CartRuleCore extends ObjectModel
public $date_add; public $date_add;
public $date_upd; public $date_upd;
protected $table = 'cart_rule'; public static $definition = array(
protected $identifier = 'id_cart_rule'; 'table' => 'cart_rule',
'identifier' => 'id_cart_rule',
);
protected $fieldsRequired = array('date_from', 'date_to'); protected $fieldsRequired = array('date_from', 'date_to');
protected $fieldsSize = array('code' => 254, 'description' => 65534); protected $fieldsSize = array('code' => 254, 'description' => 65534);
@@ -244,10 +246,10 @@ class CartRuleCore extends ObjectModel
public static function deleteByIdCustomer($id_customer) public static function deleteByIdCustomer($id_customer)
{ {
$return = true; $return = true;
$result = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'cart_rule` WHERE `id_customer` = '.(int)$id_customer); $cart_rules = new Collection('CartRule');
$cartRules = ObjectModel::hydrateCollection('CartRule', $result); $cart_rules->where('a.id_customer = '.(int)$id_customer);
foreach ($cartRules as $cartRule) foreach ($cart_rules as $cart_rule)
$return &= $cartRule->delete(); $return &= $cart_rule->delete();
return $return; return $return;
} }
@@ -636,13 +638,11 @@ class CartRuleCore extends ObjectModel
$errors = array(); $errors = array();
if (!$context) if (!$context)
$context = Context::getContext(); $context = Context::getContext();
$result = $context->cart->getCartRules(); foreach ($context->cart->getCartRules() as $cart_rule)
$cartRules = ObjectModel::hydrateCollection('CartRule', $result);
foreach ($cartRules as $cartRule)
{ {
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(); $context->cart->update();
$errors[] = $error; $errors[] = $error;
} }
+25 -3
View File
@@ -30,7 +30,7 @@
* *
* @since 1.5.0 * @since 1.5.0
*/ */
class CollectionCore implements Iterator class CollectionCore implements Iterator, Countable
{ {
/** /**
* @var string Object class name * @var string Object class name
@@ -62,6 +62,11 @@ class CollectionCore implements Iterator
*/ */
protected $iterator = 0; protected $iterator = 0;
/**
* @var bool Is current collection already hydrated
*/
protected $is_hydrated = false;
/** /**
* @param string $classname * @param string $classname
* @param int $id_lang * @param int $id_lang
@@ -79,7 +84,7 @@ class CollectionCore implements Iterator
$this->query->select('a.*'); $this->query->select('a.*');
$this->query->from($this->definition['table'].' 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']) if (isset($this->definition['multilang']) && $this->definition['multilang'])
{ {
$this->query->select('b.*'); $this->query->select('b.*');
@@ -108,6 +113,7 @@ class CollectionCore implements Iterator
public function orderBy($str) public function orderBy($str)
{ {
$this->query->orderBy($str); $this->query->orderBy($str);
return $this;
} }
/** /**
@@ -119,6 +125,7 @@ class CollectionCore implements Iterator
public function groupBy($str) public function groupBy($str)
{ {
$this->query->groupBy($str); $this->query->groupBy($str);
return $this;
} }
/** /**
@@ -129,10 +136,14 @@ class CollectionCore implements Iterator
*/ */
public function getAll($display_query = false) public function getAll($display_query = false)
{ {
if ($this->is_hydrated)
return $this;
$this->is_hydrated = true;
if ($display_query) if ($display_query)
echo $this->query.'<br />'; echo $this->query.'<br />';
$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); $this->results = ObjectModel::hydrateCollection($this->classname, $this->results, $this->id_lang);
return $this; return $this;
@@ -178,4 +189,15 @@ class CollectionCore implements Iterator
{ {
$this->iterator++; $this->iterator++;
} }
/**
* Get total of results
*
* @return int
*/
public function count()
{
$this->getAll();
return count($this->results);
}
} }
+2 -2
View File
@@ -296,9 +296,9 @@ abstract class PaymentModuleCore extends Module
$cartRulesList = ''; $cartRulesList = '';
$result = $cart->getCartRules(); $result = $cart->getCartRules();
$cartRules = ObjectModel::hydrateCollection('CartRule', $result, (int)$order->id_lang); foreach ($result as $cart_rule)
foreach ($cartRules as $cartRule)
{ {
$cartRule = $cart_rule['obj'];
$value = $cartRule->getContextualValue(true); $value = $cartRule->getContextualValue(true);
// Todo : has not been tested because order processing wasn't functionnal // 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) if ($value > $order->total_products_wt && $cartRule->partial_use == 1 && $cartRule->reduction_amount > 0)
+9 -14
View File
@@ -70,8 +70,10 @@ class ProductSupplierCore extends ObjectModel
'id_currency' => 'isUnsignedId', 'id_currency' => 'isUnsignedId',
); );
protected $table = 'product_supplier'; public static $definition = array(
protected $identifier = 'id_product_supplier'; 'table' => 'product_supplier',
'primary' => 'id_product_supplier',
);
public function getFields() public function getFields()
{ {
@@ -158,22 +160,15 @@ class ProductSupplierCore extends ObjectModel
* *
* @param int $id_product * @param int $id_product
* @param int $group_by_supplier * @param int $group_by_supplier
* @return array * @return Collection
*/ */
public static function getSupplierCollection($id_product, $group_by_supplier = true) public static function getSupplierCollection($id_product, $group_by_supplier = true)
{ {
// build query $suppliers = new Collection('ProductSupplier');
$query = new DbQuery(); $suppliers->where('a.id_product = '.(int)$id_product);
$query->select('*'); $suppliers->groupBy('a.id_supplier');
$query->from('product_supplier ps');
$query->where('ps.id_product = '.(int)$id_product);
if ($group_by_supplier) return $suppliers;
$query->groupBy('ps.id_supplier');
$results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
return ObjectModel::hydrateCollection('ProductSupplier', $results);
} }
public function delete() public function delete()
+8 -12
View File
@@ -1316,15 +1316,13 @@ class OrderCore extends ObjectModel
/** /**
* This method allows to get all Order Payment for the current order * This method allows to get all Order Payment for the current order
* @since 1.5.0.1 * @since 1.5.0.1
* @return array Collection of Order Payment * @return Collection of Order Payment
*/ */
public function getOrderPaymentCollection() public function getOrderPaymentCollection()
{ {
$order_payment = Db::getInstance()->ExecuteS(' $order_payments = new Collection('OrderPayment');
SELECT * $order_payments->where('id_order = '.(int)$this->id);
FROM `'._DB_PREFIX_.'order_payment` return $order_payments;
WHERE `id_order` = '.(int)$this->id);
return ObjectModel::hydrateCollection('OrderPayment', $order_payment);
} }
/** /**
@@ -1402,15 +1400,13 @@ class OrderCore extends ObjectModel
* *
* Get all invoices for the current order * Get all invoices for the current order
* @since 1.5.0.1 * @since 1.5.0.1
* @return array Collection of Order invoice * @return Collection of Order invoice
*/ */
public function getInvoicesCollection() public function getInvoicesCollection()
{ {
$invoices = Db::getInstance()->ExecuteS(' $order_invoices = new Collection('OrderInvoice');
SELECT * $order_invoices->where('id_order = '.(int)$this->id);
FROM `'._DB_PREFIX_.'order_invoice` return $order_invoices;
WHERE `id_order` = '.(int)$this->id);
return ObjectModel::hydrateCollection('OrderInvoice', $invoices);
} }
/** /**
+4 -2
View File
@@ -69,8 +69,10 @@ class OrderInvoiceCore extends ObjectModel
protected $fieldsRequired = array('id_order', 'number'); protected $fieldsRequired = array('id_order', 'number');
protected $fieldsValidate = array('id_order' => 'isUnsignedId', 'number' => 'isUnsignedId'); protected $fieldsValidate = array('id_order' => 'isUnsignedId', 'number' => 'isUnsignedId');
protected $table = 'order_invoice'; public static $definition = array(
protected $identifier = 'id_order_invoice'; 'table' => 'order_invoice',
'identifier' => 'id_order_invoice',
);
public function getFields() public function getFields()
{ {
+4 -2
View File
@@ -54,8 +54,10 @@ class OrderPaymentCore extends ObjectModel
'card_holder' => 'isAnything' 'card_holder' => 'isAnything'
); );
protected $table = 'order_payment'; public static $definition = array(
protected $identifier = 'id_order_payment'; 'table' => 'order_payment',
'primary' => 'id_order_payment',
);
public function getFields() public function getFields()
{ {
+4 -2
View File
@@ -82,8 +82,10 @@ class StockCore extends ObjectModel
'price_te' => 'isPrice', 'price_te' => 'isPrice',
); );
protected $table = 'stock'; public static $definition = array(
protected $identifier = 'id_stock'; 'table' => 'stock',
'identifier' => 'id_stock',
);
public function getFields() public function getFields()
{ {
+6 -11
View File
@@ -612,22 +612,17 @@ class StockManagerCore implements StockManagerInterface
* *
* @param int $id_product * @param int $id_product
* @param int $id_product_attribute * @param int $id_product_attribute
* @return array * @return Collection
*/ */
protected function getStockCollection($id_product, $id_product_attribute, $id_warehouse = null, $price_te = null) protected function getStockCollection($id_product, $id_product_attribute, $id_warehouse = null, $price_te = null)
{ {
// build query $stocks = new Collection('Stock');
$query = new DbQuery(); $stocks->where('a.id_product = '.(int)$id_product.' AND a.id_product_attribute = '.(int)$id_product_attribute);
$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);
if ($id_warehouse) if ($id_warehouse)
$query->where('s.id_warehouse = '.(int)$id_warehouse); $stocks->where('a.id_warehouse = '.(int)$id_warehouse);
if ($price_te) 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 $stocks;
return ObjectModel::hydrateCollection('Stock', $results);
} }
} }
+5 -14
View File
@@ -297,22 +297,13 @@ class SupplyOrderCore extends ObjectModel
/** /**
* Retrieves the product entries collection for the current order * 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) $details = new Collection('SupplyOrderDetail');
$id_lang = Context::getContext()->language->id; $details->where('a.id_supply_order = '.(int)$this->id);
return $details;
// 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);
} }
+4 -9
View File
@@ -187,15 +187,10 @@ class SupplyOrderDetailCore extends ObjectModel
'price_with_order_discount_te' => 'isPrice', 'price_with_order_discount_te' => 'isPrice',
); );
/** public static $definition = array(
* @var string Database table name 'table' => 'supply_order_detail',
*/ 'primary' => 'id_supply_order_detail',
protected $table = 'supply_order_detail'; );
/**
* @var string Database ID name
*/
protected $identifier = 'id_supply_order_detail';
/** /**
* @see ObjectModel::getFields() * @see ObjectModel::getFields()
+8 -12
View File
@@ -58,8 +58,10 @@ class WarehouseProductLocationCore extends ObjectModel
'id_warehouse' => 'isUnsignedId', 'id_warehouse' => 'isUnsignedId',
); );
protected $table = 'warehouse_product_location'; public static $definition = array(
protected $identifier = 'id_warehouse_product_location'; 'table' => 'warehouse_product_location',
'primary' => 'id_warehouse_product_location',
);
public function getFields() public function getFields()
{ {
@@ -122,18 +124,12 @@ class WarehouseProductLocationCore extends ObjectModel
* *
* @param int $id_product * @param int $id_product
* @param int $id_lang * @param int $id_lang
* @return array * @return Collection
*/ */
public static function getCollection($id_product) public static function getCollection($id_product)
{ {
// build query $collection = new Collection('WarehouseProductLocation');
$query = new DbQuery(); $collection->where('a.id_product = '.(int)$id_product);
$query->select('*'); return $collection;
$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);
} }
} }