这里以商品的SKU做例子:
因为商品的SKU在magento中是静态属性非eav属性,所以列表中是没有SKU这个属性的,所以要添加这个属性到列表中,这里面添加的位置有两个:
第一个方法:
$category = Mage::getModel('catalog/category')->load($category_id);
$products = $category->getProductCollection();
这里是通过分类来得到分类下的商品集合,所以在得到商品集合这里面进行添加SKU属性:
这里调用的方法是getProductCollection()
public function getProductCollection()
{
$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($this);
return $collection;
}
该方法里面涉及到了Mage_Catalog_Model_Resource_Product_Collection类,在调用该Collection资源类的时候会自动调用_construct类,对资源模型进行初始化:
/**
* Initialize resources
*
*/
protected function _construct()
{
if ($this->isEnabledFlat()) {
$this->_init('catalog/product', 'catalog/product_flat');
}
else {
$this->_init('catalog/product');
}
$this->_initTables();
}
这里面又调用了_init()方法:
/**
* Standard resource collection initalization
*
* @param string $model
* @param unknown_type $entityModel
* @return Mage_Catalog_Model_Resource_Product_Collection
*/
protected function _init($model, $entityModel = null)
{
if ($this->isEnabledFlat()) {
$entityModel = 'catalog/product_flat';
}
return parent::_init($model, $entityModel);
}
继承父类是:
/**
* Standard resource collection initalization
*
* @param string $model
* @return Mage_Core_Model_Mysql4_Collection_Abstract
*/
protected function _init($model, $entityModel = null)
{
$this->setItemObjectClass(Mage::getConfig()->getModelClassName($model));
if ($entityModel === null) {
$entityModel = $model;
}
$entity = Mage::getResourceSingleton($entityModel);
$this->setEntity($entity);
return $this;
}
这里设置了模型和资源模型。大多数集合继承的抽象数据库集合_initSelect()在其__construct()方法的末尾调用一个方法(也是在_constrct()调用之后)。
框架是magento,默认把init()定义为初始化函数,那么magento会在执行其他程序之前执行init()函数,这实际上和php内置的构造函数有异曲同工的效果了. 在这种情况下你可以只定义init() 函数, 也可以只定义 __construct() 函数, 或者二者兼有.
(备注: __construct() 是PHP内置的构造函数, 是同 PHP 解析引擎自动调用的, 而 init() 则是由 PHP 框架自动调用的.)
/**
* Initialize factory
*
* @param Mage_Core_Model_Resource_Abstract $resource
* @param array $args
*/
public function __construct($resource = null, array $args = array())
{
parent::__construct($resource);
$this->_factory = !empty($args['factory']) ? $args['factory'] : Mage::getSingleton('catalog/factory');
}
继承父类:
/**
* Collection constructor
*
* @param Mage_Core_Model_Resource_Abstract $resource
*/
public function __construct($resource = null)
{
parent::__construct();
$this->_construct();
$this->setConnection($this->getEntity()->getReadConnection());
$this->_prepareStaticFields();
$this->_initSelect();
}
这里在最后调用了_initSelect(),该_initSelect方法设置集合的选择查询的主表。
/**
* Initialize collection select
* Redeclared for remove entity_type_id condition
* in catalog_product_entity we store just products
*
* @return Mage_Catalog_Model_Resource_Product_Collection
*/
protected function _initSelect()
{
if ($this->isEnabledFlat()) {
$this->getSelect()
->from(array(self::MAIN_TABLE_ALIAS => $this->getEntity()->getFlatTableName()), null)
->where('e.status = ?', new Zend_Db_Expr(Mage_Catalog_Model_Product_Status::STATUS_ENABLED));
$this->addAttributeToSelect(array('entity_id', 'type_id', 'attribute_set_id'));
if ($this->getFlatHelper()->isAddChildData()) {
$this->getSelect()
->where('e.is_child = ?', 0);
$this->addAttributeToSelect(array('child_id', 'is_child'));
}
} else {
$this->getSelect()->from(array(self::MAIN_TABLE_ALIAS => $this->getEntity()->getEntityTable()));
}
return $this;
}
在$this->addAttributeToSelect(array(‘entity_id’, ‘type_id’, ‘attribute_set_id’));可以添加想要添加的属性。这里也涉及到了magento中资源模型。
第二个方法:
Mage::getModel('catalog/layer')->prepareProductCollection($products);
这里调用prepareProductCollection()方法:
/**
* Initialize product collection
*
* @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
* @return Mage_Catalog_Model_Layer
*/
public function prepareProductCollection($collection)
{
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addTaxPercents();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
return $this;
}
在这里可以在Collection集合类中添加sku属性:
public function prepareProductCollection($collection)
{
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addTaxPercents()
->addAttributeToSelect('sku');
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
return $this;
}
通过该方法也可以在商品对象中添加上sku属性