2011-10-14 17:10:00
如果你想在magento中手动写sql语句 (in the .php code files or .phtml template files), 可以按如下格式:
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$connection->query("insert into table name (fields) values (values) ");
比如你可以这样来取:
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $connection->query("select * from table");
while ($row = $result->fetch() ) {
$ids[]=$row['id'];
}
打印sql语句可以有2种方法:
一是获取所有sql query,从zend_db中去echo。进入到lib/Zend/Db/Adapter/Abstract.php目录
像这样加一些临时调试语句
public function query($sql, $bind = array())
{
// connect to the database if needed
$this->_connect();
// is the $sql a Zend_Db_Select object?
if ($sql instanceof Zend_Db_Select) {
if (empty($bind)) {
$bind = $sql->getBind();
}
$sql = $sql->assemble();
}
echo '$sql' . "\n<br />\n";
var_dump($bind);
}
二是通过magento自带的两种sql调试方法$query=$collection->getSelectSql(true);
echo $query;
$collection->printlogquery(true);