Magento执行SQL操作数据库

作者:Jeen 发布于:2013-4-21 19:51 Sunday 分类:工作笔记

一、使用Magento  resource 连接数据库,进行操作

  1. $db = Mage::getResourceSingleton(‘core/resource’)->getConnection(‘core_write’);
  2. $result = $db->query(‘SELECT `entity_id` FROM `catalog_product_entity`’);

  3. if(!$result) {
  4.     return FALSE;
  5. }

  6. $rows = $result->fetch(PDO::FETCH_ASSOC);

  7. if(!$rows) {
  8.     return FALSE;
  9. }

  10. print_r($rows);

二、针对模块,采用zend 框架模式操作模块对应数据表格

  1. $resource = Mage::getSingleton(‘core/resource’);
  2. $read = $resource->getConnection(‘core_read’);
  3. $categoryProductTable=$read->getTableName(‘category/category_product’)
  4. $select = $read->select()->from(array(‘cp’=>$categoryProductTable))->where(‘cp.category_id=?’, ’3′);
  5. $products=$read->fetchAll($select);
  6. foreach($products as $row)
  7. {
  8. $product = Mage::getModel(‘catalog/product’)->load($row['product_id']);
  9. echo $product->getName();
  10. }

 

标签: magento

发表评论:

©2010-2024 Jeen All Rights Reserved.Powered by emlog 京ICP备15058100号-1