`
coolsooner
  • 浏览: 1312797 次
文章分类
社区版块
存档分类
最新评论

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;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics