码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-08-27 15:07:17      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

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类

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

 

第二行调用$batchModel->setParams()

很显然会到app\code\core\Mage\Dataflow\Model\Batch.php文件中去找

/**
* Set additional params
* automatic convert to serialize data
*
* @param mixed $data
* @return Mage_Dataflow_Model_Batch_Abstract
*/
public function setParams($data)
{
$this->setData(‘params‘, serialize($data));
return $this;
}

发现调用setData()方法

此类没有,去他的父类找

class Mage_Dataflow_Model_Batch extends Mage_Core_Model_Abstract

app\code\core\Mage\Core\Model\Abstract.php

发现没有,他继承的是

abstract class Mage_Core_Model_Abstract extends Varien_Object

找到对应核心类

lib\Varien\Object.php

找到

/**
* Overwrite data in the object.
*
* $key can be string or array.
* If $key is string, the attribute value will be overwritten by $value
*
* If $key is an array, it will overwrite all the data in the object.
*
* @param string|array $key
* @param mixed $value
* @return Varien_Object
*/
public function setData($key, $value=null)
{
$this->_hasDataChanges = true;
if(is_array($key)) {
$this->_data = $key;
$this->_addFullNames();
} else {
$this->_data[$key] = $value;
if (isset($this->_syncFieldsMap[$key])) {
$fullFieldName = $this->_syncFieldsMap[$key];
$this->_data[$fullFieldName] = $value;
}
}
return $this;
}

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

标签:

原文地址:http://www.cnblogs.com/you-jia/p/4763171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!