magento 上传csv表格中实例化对象例子

app\code\core\Mage\Dataflow\Model\Convert\Parser\csv.php

文件是后台上传csv,插入到dataflow_batch_import中转表的代码,有如下代码片段

1.$batchModel = $this->getBatchModel();

2.$batchModel->setParams($this->getVars())
->setAdapter($adapterName)
->save();

从上往下看,

第一行$this->getBatchModel();

找到对应文件

app\code\core\Mage\Dataflow\Model\Convert\Parser\Abstract.php

/**
* Retrieve Batch model singleton
*
* @return Mage_Dataflow_Model_Batch
*/
public function getBatchModel()
{
if (is_null($this->_batch)) {
$this->_batch = Mage::getSingleton('dataflow/batch');
}
return $this->_batch;
}

 

调用了Mage::getSingleton方法,再继续找对应类

app\Mage.php

/**
* Retrieve model object singleton
*
* @param string $modelClass
* @param array $arguments
* @return Mage_Core_Model_Abstract
*/
public static function getSingleton($modelClass='', array $arguments=array())
{
$registryKey = '_singleton/'.$modelClass;
if (!self::registry($registryKey)) {
self::register($registryKey, self::getModel($modelClass, $arguments));
}
return self::registry($registryKey);
}

发现最终还是会调用此类中getModel()方法

public static function getModel($modelClass = '', $arguments = array())
{
return self::getConfig()->getModelInstance($modelClass, $arguments);
}

调用此类getConfig()

/**
* Retrieve a config instance
*
* @return Mage_Core_Model_Config
*/
public static function getConfig()
{
return self::$_config;
}

会返回Mage_Core_Model_Config的一个对象,且调用getModelInstance()

对应文件是:app\code\core\Mage\Core\Model\Config.php

/**
* Get model class instance.
*
* Example:
* $config->getModelInstance('catalog/product')
*
* Will instantiate Mage_Catalog_Model_Mysql4_Product
*
* @param string $modelClass
* @param array|object $constructArguments
* @return Mage_Core_Model_Abstract|false
*/
public function getModelInstance($modelClass='', $constructArguments=array())
{
$className = $this->getModelClassName($modelClass);
if (class_exists($className)) {
Varien_Profiler::start('CORE::create_object_of::'.$className);
$obj = new $className($constructArguments);
Varien_Profiler::stop('CORE::create_object_of::'.$className);
return $obj;
} else {
return false;
}
}

会判断这个类是否存在,不存在就会new一个

传递的参数为dataflow/batch

会调用getModelClassName判断是否存在

public function getModelClassName($modelClass)
{
$modelClass = trim($modelClass);
if (strpos($modelClass, '/')===false) {
return $modelClass;
}
return $this->getGroupedClassName('model', $modelClass);
}

可以看到如果是带有斜杠,说明此类没有实例化,会new一下

返回Mage_Dataflow_Model_Batch类

此时第一行代码结束,获得对应的对象

 

posted @ 2015-08-27 14:19  Forward1990  阅读(283)  评论(0编辑  收藏  举报