model--resourceModel---collection 初始化

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;

    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值