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

继承---Mage_core_model_mysql4_abstract的类的数据库操作方法------------继承Mage_core_model_mysql4_collection_abstract的类的的数据库操作方法

 
阅读更多

继承Mage_core_model_mysql4_abstract.php的class

里面的函数操作数据库:
1
查询:
$select = $this->_getReadAdapter()->select()
->from($this->getTable('profile/profile_store'))
->where('profile_id = ?', $object->getId());
$data = $this->_getReadAdapter()->fetchAll($select);
$data是个数组。
2
删除语法例子:

$condition = $this->_getWriteAdapter()->quoteInto('profile_id = ?', $object->getId());
$this->_getWriteAdapter()->delete($this->getTable('profile/profile_store'), $condition);
3
插入数据的例子:
$storeArray = array();
$storeArray['profile_id'] = $object->getId();
$storeArray['store_id'] = $store;
$this->_getWriteAdapter()->insert($this->getTable('profile/profile_store'), $storeArray);


4
重新得到!!select object------>zend_Db_select!!
/**
* Retrieve select object for load object data
*
* @param string $field
* @param mixed $value
* @return Zend_Db_Select
*/
protected function _getLoadSelect($field, $value, $object)
{
$select = parent::_getLoadSelect($field, $value, $object);

if ($object->getStoreId()) {
$select->join(
array('cps' => $this->getTable('profile/profile_store')),
$this->getMainTable().'.profile_id = `cps`.profile_id'
)
->where('is_active=1 AND `cps`.store_id in (' . Mage_Core_Model_App::ADMIN_STORE_ID . ', ?) ',

$object->getStoreId())
->order('store_id DESC')
->limit(1);
}
return $select;
}


************************************************************************************
继承
Mage_Core_Model_Mysql4_Collection_Abstract的类里面的函数操作数据库
collection。

1
$select = $this->getConnection()->select()
->from($this->getTable('profile/profile_store'))
->where($this->getTable('profile/profile_store').'.profile_id IN (?)', $items);

$result = $this->getConnection()->fetchPairs($select)

//
/**
* Fetches all SQL result rows as an array of key-value pairs.
*
* The first column is the key, the second column is the
* value.
*
* @param string|Zend_Db_Select $sql An SQL SELECT statement.
* @param mixed $bind Data to bind into SELECT placeholders.
* @return array
*/
public function fetchPairs($sql, $bind = array()

result为数组。

2
$this->getSelect()->join(
array('store_table' => $this->getTable('profile/profile_store')),
'main_table.profile_id = store_table.profile_id',
array()
)
->where('store_table.store_id in (?)', ($withAdmin ? array(0, $store) : $store))
->group('main_table.profile_id');

通过getSelect()方法:@return Varien_Db_Select
$this->getConnection()->select(),得到的也是Varien_Db_Select
但是getSelect加上了一些数据的初始化过程,故在join这方面的操作,要使用getSelect()得到Varien_db_select方法。



明天研究Mage_core_model_mysql4_abstract和Mage_core_model_mysql4_collection_abstract这两个系统class!!!











分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics