一、使用Magento resource 连接数据库,进行操作
- $db = Mage::getResourceSingleton('core/resource')->getConnection('core_write');
- $result = $db->query('SELECT `entity_id` FROM `catalog_product_entity`');
- if(!$result) {
- return FALSE;
- }
- $rows = $result->fetch(PDO::FETCH_ASSOC);
- if(!$rows) {
- return FALSE;
- }
- print_r($rows);
二、针对模块,采用zend 框架模式操作模块对应数据表格
- $resource = Mage::getSingleton('core/resource');
- $read = $resource->getConnection('core_read');
- $categoryProductTable=$read->getTableName('category/category_product')
- $select = $read->select()->from(array('cp'=>$categoryProductTable))->where('cp.category_id=?', '3');
- $products=$read->fetchAll($select);
- foreach($products as $row)
- {
- $product = Mage::getModel('catalog/product')->load($row['product_id']);
- echo $product->getName();
- }