博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
model--resourceModel---collection 初始化
阅读量:4200 次
发布时间:2019-05-26

本文共 4585 字,大约阅读时间需要 15 分钟。

model

不是eav模型的初始化。

1

class RichardMason_Profile_Model_Profile extends Mage_Core_Model_Abstract{

    public function _construct()

    {

        parent::_construct();

        $this->_init('profile/profile');

    }

1.1

    parent::parent为空;

 

 

1.2

//abstract class Mage_Core_Model_Abstract extends Varien_Object

 /**

     * Standard model initialization

     *

     * @param string $resourceModel

     * @param string $idFieldName

     * @return Mage_Core_Model_Abstract

     */

    protected function _init($resourceModel)

    {

        $this->_setResourceModel($resourceModel);

    }

 

 

1.2.1

//abstract class Mage_Core_Model_Abstract extends Varien_Object

  /**

     * Set resource names

     *

     * If collection name is ommited, resource name will be used with _collection appended

     *

     * @param string $resourceName

     * @param string|null $resourceCollectionName

     */

    protected function _setResourceModel($resourceName, $resourceCollectionName=null)

    {

        $this->_resourceName = $resourceName;

        if (is_null($resourceCollectionName)) {

            $resourceCollectionName = $resourceName.'_collection';

        }

        $this->_resourceCollectionName = $resourceCollectionName;

    }

 

故可以看出Model/profile.php的初始化执行init方法,是一个赋值的过程。

 

将变量$_moduleName赋值为profile/profile,$_resourceCollectionName赋值为

 

profile/profile_collection。

 

1>对于$_resourceName,文件路径设定在config.xml中,

<profile_mysql4>

                <class>RichardMason_Profile_Model_Mysql4</class>

 

2>$_resourceCollectionName,设定在本函数的传入参数:$resourceCollectionName,如果为空,则使

 

用默认规则。 $resourceCollectionName = $resourceName.'_collection'

 

 

 

2

model/mysql4/profile.php

初始化:

 public function _construct()

    {    

        // Note that the profiles_id refers to the key field in your database table.

        $this->_init('profile/profile', 'profile_id');

    }

 

2.1

//abstract class Mage_Core_Model_Mysql4_Abstract extends Mage_Core_Model_Resource_Abstract

 /**

     * Standard resource model initialization

     *

     * @param string $mainTable

     * @param string $idFieldName

     * @return Mage_Core_Model_Mysql4_Abstract

     */

    protected function _init($mainTable, $idFieldName)

    {

        $this->_setMainTable($mainTable, $idFieldName);

    }

 

2.2

//abstract class Mage_Core_Model_Mysql4_Abstract extends Mage_Core_Model_Resource_Abstract

 

 /**

     * Set main entity table name and primary key field name

     *

     * If field name is ommited {table_name}_id will be used

     *

     * @param string $mainTable

     * @param string|null $idFieldName

     * @return Mage_Core_Model_Mysql4_Abstract

     */

    protected function _setMainTable($mainTable, $idFieldName=null)

    {

        $mainTableArr = explode('/', $mainTable);

 

        if (!empty($mainTableArr[1])) {

            if (empty($this->_resourceModel)) {

                $this->_setResource($mainTableArr[0]);

            }

            $this->_setMainTable($mainTableArr[1], $idFieldName);

        } else {

            $this->_mainTable = $mainTable;

            if (is_null($idFieldName)) {

                $idFieldName = $mainTable.'_id';

            }

            $this->_idFieldName = $idFieldName;

        }

 

        return $this;

    }

 

为表,id赋值的过程。

 

2.2.1

  /**

     * Resource model name that contains entities (names of tables)

     *

     * @var string

     */

    protected $_resourceModel;

 

 $this->_mainTable为最后那个反斜杠后面的字符,id为$idFieldName

 

 

 

3

class RichardMason_Profile_Model_Mysql4_Profile_Collection extends 

 

Mage_Core_Model_Mysql4_Collection_Abstract

 

 public function _construct()

    {

        parent::_construct();

        $this->_init('profile/profile');

 

        $this->_map['fields']['profile_id'] = 'main_table.profile_id';

    }

3.1

$this->_init('profile/profile');

//abstract class Mage_Core_Model_Mysql4_Collection_Abstract extends 

 

Varien_Data_Collection_Db

 /**

     * Standard resource collection initalization

     *

     * @param string $model

     * @return Mage_Core_Model_Mysql4_Collection_Abstract

     */

    protected function _init($model, $resourceModel=null)

    {

        $this->setModel($model);

        if (is_null($resourceModel)) {

            $resourceModel = $model;

        }

        $this->setResourceModel($resourceModel);

        return $this;

    }

 

3.1.1

//$this->_model   =profile

$this->_itemObjectClass的赋值

 

$this->setModel($model);

 

 /**

     * Set model name for collection items

     *

     * @param string $model

     * @return Mage_Core_Model_Mysql4_Collection_Abstract

     */

    public function setModel($model)

    {

        if (is_string($model)) {

            $this->_model = $model;

            $this->setItemObjectClass(Mage::getConfig()->getModelClassName($model));

        }

        return $this;

    }

 

3.1.1.1

 /**

     * Set collection item class name

     *

     * @param   string $className

     * @return  Varien_Data_Collection

     */

    function setItemObjectClass($className)

    {

        $className = Mage::getConfig()->getModelClassName($className);

        /**

         * is_subclass_of($className, 'Varien_Object') - Segmentation fault in php 5.2.3

         */

        /*if (!is_subclass_of($className, 'Varien_Object')) {

            throw new Exception($className.' does not extends from Varien_Object');

        }*/

        $this->_itemObjectClass = $className;

        return $this;

    }

 

 

 

 

3.1.2

$this->setResourceModel($resourceModel);

 public function setResourceModel($model)

    {

        $this->_resourceModel = $model;

    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:http://swcli.baihongyu.com/

你可能感兴趣的文章
JQuery 简介
查看>>
Java创建对象的方法
查看>>
Extjs自定义组件
查看>>
TreeGrid 异步加载节点
查看>>
Struts2 标签库讲解
查看>>
Google Web工具包 GWT
查看>>
材料与工程学科相关软件
查看>>
MPI的人怎么用仪器
查看>>
windows 下AdNDP 安装使用
查看>>
Project 2013项目管理教程(1):项目管理概述及预备
查看>>
ssh客户端后台运行
查看>>
哥去求职,才说了一句话考官就让我出去
查看>>
【React Native】把现代web科技带给移动开发者(一)
查看>>
【GoLang】Web工作方式
查看>>
Launch Sublime Text 3 from the command line
查看>>
【数据库之mysql】mysql的安装(一)
查看>>
【数据库之mysql】 mysql 入门教程(二)
查看>>
【HTML5/CSS/JS】A list of Font Awesome icons and their CSS content values(一)
查看>>
【HTML5/CSS/JS】<br>与<p>标签区别(二)
查看>>
【HTML5/CSS/JS】开发跨平台应用工具的选择(三)
查看>>