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


    appcodecoreMageDataflowModelConvertParsercsv.php

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

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

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

    从上往下看,

    第一行$this->getBatchModel();

    找到对应文件

    appcodecoreMageDataflowModelConvertParserAbstract.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方法,再继续找对应类

    appMage.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()

    对应文件是:appcodecoreMageCoreModelConfig.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类

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

  • 相关阅读:
    [多线程]使用信号量进行同步
    [多线程]互斥锁与信号量的区别
    [多线程]环形缓冲区以及多线程条件同步
    [多线程]LINUX c多线程编程-线程初始化与建立
    [STL]bitset使用
    [算法]败者树
    【Rollo的Python之路】Python:字符串内置函数
    【Rollo的Python之路】Python:字典的学习笔记
    【Rollo的Python之路】 购物车程序练习
    【Rollo的Python之路】Python 元组的学习
  • 原文地址:https://www.cnblogs.com/you-jia/p/4763171.html
Copyright © 2020-2023  润新知