// 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)
{
$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;
+11 -11
View File
@@ -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;
}
+25 -3
View File
@@ -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.'<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);
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);
}
}
+2 -2
View File
@@ -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)
+9 -14
View File
@@ -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()
+8 -12
View File
@@ -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;
}
/**
+4 -2
View File
@@ -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()
{
+4 -2
View File
@@ -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()
{
+4 -2
View File
@@ -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()
{
+6 -11
View File
@@ -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;
}
}
+5 -14
View File
@@ -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;
}
+4 -9
View File
@@ -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()
+8 -12
View File
@@ -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;
}
}