Last active
August 29, 2015 14:01
-
-
Save SchumacherFM/715a89967ab95a6b25b2 to your computer and use it in GitHub Desktop.
Singelton Collection Pattern #Magento
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'], | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Second implementation is more generic