您的位置:首页 > 数据库

magento ------------------连接数据库-----进行操作的方法,方式!!!精***

2011-02-25 16:31 447 查看
magento连接数据库的方法:

1

简单方式。限制性小

1.1

$write = Mage::getSingleton('core/resource')->getConnection('core_write');

$readresult=$write->query("SELECT eav_attribute_option_value.option_id, eav_attribute_option_value.value FROM eav_attribute_option_value

INNER JOIN eav_attribute_option ON eav_attribute_option_value.option_id=eav_attribute_option.option_id WHERE

eav_attribute_option.attribute_id=505 AND eav_attribute_option_value.option_id IN (".$brands.")");

while ($row = $readresult->fetch() ) {

$brandlist[] = array('value'=>$row['option_id'], 'label'=>$row['value']);

}

1.2

$resource = Mage::getSingleton('core/resource');

$read = $resource->getConnection('catalog_read');

$select = $read->select()

//得到zend_db_select

->from(array('cp'=>$categoryProductTable))

->join(array('pei'=>$productEntityIntTable),'pei.entity_id=cp.product_id', array())

->joinNatural(array('ea'=>$eavAttributeTable))

->where('cp.category_id=?', $categoryId)

->where('pei.value=1')

->where('ea.attribute_code="terry"');

$rows = $read->fetchAll($select);

$ids = array();

foreach($rows AS $row) {

$ids[] = $row['product_id'];

}

$ret = implode(',', $ids);

return $ids;

2

一般用于magento,产品,分类等信息的修改。

通过

此处一般说的是eav模型中的collection

Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection

只要实现了下面的类的类,

Varien_Data_Collection

都可以使用

public function getSelect()

{

$this->_select = $this->_conn->select();

return $this->_select;

}

得到 Varien_Db_Select。

3

对于单个产品的查询和修改

根据ID值。或者唯一值。

$model = Mage::getModel('blog/blog')->load($id);

该方式信息比较全面。

4

$coll = Mage::getModel('blog/tag')->getCollection();

$sel = $coll->getSelect();

$coll->getSelect()

5

对于Mage::getResourceModel()

和Mage::getModel()->getCollection();

得到的都是Get collection instance

_setResourceModel($resourceName, $resourceCollectionName=null)



5.1 Mage::getResourceModel()

和Mage::getModel()->getCollection()

和Eav模型,都可以使用getSelect();

6

Mage_Core_Model_Mysql4_Abstract

1

非eav模型:

1.1

resource collection model:

AW_Blog_Model_Mysql4_Blog_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract

Mage_Core_Model_Mysql4_Collection_Abstract extends Varien_Data_Collection_Db

Varien_Data_Collection_Db extends Varien_Data_Collection

1.2

resource model

AW_Blog_Model_Mysql4_Blog extends Mage_Core_Model_Mysql4_Abstract

Mage_Core_Model_Mysql4_Abstract extends Mage_Core_Model_Resource_Abstract

2

对于eav模型:

2.1

resource collection model:

Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collectionextends Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract

Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract extends Mage_Eav_Model_Entity_Collection_Abstract

Mage_Eav_Model_Entity_Collection_Abstract extends Varien_Data_Collection_Db

Varien_Data_Collection_Db extends Varien_Data_Collection

2.2

resource model

class Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract extends Mage_Eav_Model_Entity_Abstract

Mage_Eav_Model_Entity_Abstract

extends Mage_Core_Model_Resource_Abstract

implements Mage_Eav_Model_Entity_Interface

model--->

_getResource(得到resource model)

在resource中,可以使用的方法:

_getReadAdapter()

_getWriteAdapter()---->进而可以通过方法 public function select()得到Varien_Db_Select;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: