在Magento中直接使用SQL语句

来源:互联网 发布:java map 红黑树 编辑:程序博客网 时间:2024/05/17 03:53

有时希望在Magento模块中写一些SQL语句,绕过资源模型,可以这样

1.

protected function _getResource(){return Mage::getSingleton('core/resource');}


2.插入操作

$table = $this->_getResource()->getTableName('supplier_serviceprice'); $this->_getResource()->getConnection('core_write')->insert($table, array('product_id' => $productId, "supplier_id" =>$receiverSuppliers[$i],"service_price" => $receiverPrice[$i],"is_primary"=>$primaryReceiver[$i]));

3.删除操作

$table = $this->_getResource()->getTableName('supplier_serviceprice'); $this->_getResource()->getConnection('core_write')->delete($table, $this->_getResource()->getConnection('core_write')->quoteInto('product_id=?', $this->_getProduct()->getId()));


另一种方法

$read = Mage::getSingleton ( 'core/resource' )->getConnection ( 'core_read' );$results = $read->fetchAll("SELECT supplier_paypal,service_price,is_primary FROM supplier_serviceprice ss    INNER JOIN supplier s ON ss.supplier_id = s.supplier_id");echo $results['字段名']}