在做开发时有的时候迷惑,Magento都提供了数据的对象,那该如何查询自定义的SQL语句呢.
其实Magento已经想过这个问题了.
只需instance core/resource的对象进行connect就能得到数据访问的对象了.语法是跟Zend_Db一致的.
看代码:
<?php
//select query
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
//make connection
$qry = "select name FROM user_data WHERE id=1 LIMIT 1 "; //query
$res = $read->fetchRow($qry); //fetch row
$name = $res['name']; //outputs name
//other form
$qry = "select name FROM user_data WHERE namer LIKE '%a%' ";//query
$res = $read->fetchAll($qry); //get array
//Insert query
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "INSERT INTO user_data values (?,?)"; //insert query
$write->query($sql, array('name','pass')); //write to database
?>
像fetch fetchall这样的方法是不是以前经常用到,没错.Zend_Db同样和PDO的风格差不多.
(责任编辑:好模板) |