zoukankan      html  css  js  c++  java
  • 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类

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

  • 相关阅读:
    小白学Python(13)——pyecharts 绘制 柱状图/条形图 Bar
    小白学Python(12)——pyecharts ,生成词云图 WordCloud
    小白学Python(11)——pyecharts,绘制饼图 Pie
    小白学Python(10)——pyecharts 绘制仪表图 Gauge
    小白学Python(9)——pyecharts 绘制漏斗图 Funnel
    小白学Python(8)——pyecharts 入门
    小白学Python(7)——利用Requests下载网页图片、视频
    小白学Python(6)——python-pptx 添加图表
    python2/3 利用psycopg2 连接postgreSQL数据库。
    linux debian 9 / centos 7配置postgresSQL数据库
  • 原文地址:https://www.cnblogs.com/you-jia/p/4763171.html
Copyright © 2011-2022 走看看