ITeye博客

magento -- 在magento下如何直接操作数据库

mypages · 2009-09-21 19:16 · magento · SQL · Markdown

magento -- 在magento下如何直接操作数据库

在magento中,要操作数据库,首先你需要一个resource model(翻译成"资源模型",magento中把数据库模型类称为一种资源(当然是相对程序来说的)),然后通过这个resource获取一个对数据库的连接.这样你就得到了一个Varien PDO Adapter对象,通过这个对象就可以直接通过sql语句操作.

view plaincopy to clipboardprint?
$w = Mage::getSingleton(’core/resource’)->getConnection(’core_write’);  
$result = $w->query(’select ’entity_id’ from ’catalog_product_entity’);  
if (!$result) {  
return false;  
}  
$row = $result->fetch(PDO::FETCH_ASSOC);  
if (!$row) {  
return false;  
} 
$w = Mage::getSingleton(’core/resource’)->getConnection(’core_write’);
$result = $w->query(’select ’entity_id’ from ’catalog_product_entity’);
if (!$result) {
return false;
}
$row = $result->fetch(PDO::FETCH_ASSOC);
if (!$row) {
return false;
}

 

 

 

 

想通过订单ID,来查询‘支付方式’和‘订单总价’:

这个功能MAGENTO已经有这样的接口了,你可以直接调用而不必采取这种方式。

$order = Mage::getModel('sales/order')->load('order_id');//取出订单号为order_id的订单
$order->getPayment();
...

//获取此订单的支付方式,同样的订单总价也有相应的方法来获取

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。