Magento 操作数据库的增删改查

第一种方法,直接写sql

查:

    $read = Mage::getSingleton("core/resource")->getConnection('core_read');//查询用core_read
    $sql = "select * from `abc`";    
    $result = $read->fetchAll($sql);  //fetchRow查找一条

增,删,改

    $write = Mage::getSingleton("core/resource")->getConnection('core_write');//增删改用core_write
    $sql = "insert into abc (name)values('hello')";
    $write->query($sql);

另一种写法:

	增
    $write = Mage::getSingleton("core/resource")->getConnection('core_write');
    $table = Mage::getSingleton('core/resource')->getTableName('abc');
    $write->insert($table,array('name'=>'hello'));
 
	改
    $write = Mage::getSingleton("core/resource")->getConnection('core_write');
    $table = Mage::getSingleton('core/resource')->getTableName('abc');
    $write->update($table,array('name'=>'abc'),array('id=?'=>3));
 
	删
    $write = Mage::getSingleton("core/resource")->getConnection('core_write');
    $table = Mage::getSingleton('core/resource')->getTableName('abc');
    $write->delete($table,array('id=?'=>3));
 
	查
    $read = Mage::getSingleton("core/resource")->getConnection('core_read');
    $table = Mage::getSingleton('core/resource')->getTableName('abc');
    $result = $read->select()->from(array('main_table'=>$table))->where('main_table.id=?',3)->limit(1);
	//$result = $read->select()->from($table)->where("sup_id = 10000 and postage_type = 0")->order('id DESC')->limit(1);
    $products=$read->fetchAll($result); // 返回所有行
	//$products=$read->fetchRow($select);   //返回行

排序相关

	$oCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->setOrder('name_of_attribute_to_sort', 'ASC');

	or

	$oCollection = Mage::getModel('catalog/product')->getCollection();
$oCollection
    ->getSelect()
    ->order(array('SUBSTRING(field, 1, 2) DESC'))
    ->limit(10);

版权属于: sbboke版权所有。

转载时必须以链接形式注明作者和原始出处及本声明。

张贴在magentomagento教程标签:

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论