Skip to content

Instantly share code, notes, and snippets.

@SchumacherFM
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SchumacherFM/715a89967ab95a6b25b2 to your computer and use it in GitHub Desktop.
Save SchumacherFM/715a89967ab95a6b25b2 to your computer and use it in GitHub Desktop.
Singelton Collection Pattern #Magento
<?php
class Zookal_Core_Helper_Data extends Mage_Core_Helper_Abstract
{
private $_objectCache = [];
public function getCollectionSingleton($modelClass, array $collectionOptions = null, $cacheKey = null)
{
$cacheKey = null === $cacheKey ? $modelClass . '/' . json_encode($collectionOptions) : (string)$cacheKey;
// $cacheKey = md5($cacheKey); if preferred
if (isset($this->_objectCache[$cacheKey])) {
return $this->_objectCache[$cacheKey];
}
/** @var Varien_Data_Collection_Db $collection */
$collection = Mage::getResourceModel($modelClass);
if(false === $collection) {
return false;
}
$hasAddAttributeToFilter = method_exists($collection, 'addAttributeToFilter');
if ($hasAddAttributeToFilter && isset($collectionOptions['atf'])) {
foreach ($collectionOptions['atf'] as $attribute => $condition) {
$collection->addAttributeToFilter($attribute, $condition);
}
}
$hasAddAttributeToSelect = method_exists($collection, 'addAttributeToSelect');
if ($hasAddAttributeToSelect && isset($collectionOptions['ats'])) {
foreach ($collectionOptions['ats'] as $attribute) {
$collection->addAttributeToSelect($attribute);
}
}
$this->_objectCache[$cacheKey] = $collection;
return $collection;
}
/**
* @return Mage_Catalog_Model_Resource_Product_Collection
*/
public function getProducts()
{
return $this->getCollectionSingleton('catalog/product_collection', [
'atf' => ['sku', array('in' => 'ottoman')],
'ats' => 'name',
]);
}
}
<?php
class Zookal_Core_Helper_Data extends Mage_Core_Helper_Abstract
{
private $_objectCache = [];
/**
* @param string $modelClass
* @param array $collectionOptions
* @param null $cacheKey
*
* @return bool|Varien_Data_Collection_Db
*/
public function getCollectionSingleton($modelClass, array $collectionOptions = null, $cacheKey = null)
{
$cacheKey = null === $cacheKey ? $modelClass . '/' . json_encode($collectionOptions) : (string)$cacheKey;
// $cacheKey = md5($cacheKey); if preferred
if (isset($this->_objectCache[$cacheKey])) {
return $this->_objectCache[$cacheKey];
}
/** @var Varien_Data_Collection_Db $collection */
$collection = Mage::getResourceModel($modelClass);
if (false === $collection) {
return false;
}
foreach ($collectionOptions as $method => $arguments) {
if (true === method_exists($collection, $method)d) {
call_user_func_array([$collection, $method], $arguments);
}
}
$this->_objectCache[$cacheKey] = $collection;
return $collection;
}
/**
* @return Mage_Catalog_Model_Resource_Product_Collection
*/
public function getProducts()
{
return $this->getCollectionSingleton('catalog/product_collection', [
'addAttributeToFilter' => ['sku', array('in' => 'ottoman')],
'addAttributeToSelect' => ['name'],
]);
}
}
@SchumacherFM
Copy link
Author

Second implementation is more generic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment