首页 > 其他 > 详细

magento CURD操作

时间:2014-07-25 23:51:50      阅读:296      评论:0      收藏:0      [点我收藏+]

查询:

$model = Mage::getModel(‘mynews/mynews‘); 
$collection = $model->getCollection(); 
$collection->addAttributeToSelect(array(‘name‘, ‘image‘, ‘url_key‘)) //选择要select的属性
  ->setPageSize(20)//获取的个数
  ->setCurPage(1)
  ->addAttributeToFilter(‘style‘, ‘brass‘);//属性过滤 详细文档 http://justcoding.iteye.com/blog/1681318
  ->addAttributeToSort(‘name‘, ‘ASC‘);//排序
$collection->getSize();
foreach($collection as $item){ print_r($item->getData()); print_r($item->getTitle()); }

创建:

$data = array(‘title‘=>‘hello there‘,‘content‘=>‘how are you? i am fine over here.‘,‘status‘=>1); 
$model = Mage::getModel(‘mynews/mynews‘)->setData($data);
try { 
   $insertId = $model->save()->getId(); echo "Data successfully inserted. Insert ID:".$insertId; 
} catch (Exception $e){ 
   echo $e->getMessage(); 
}

更新:

$id = 2; 
$data = array(‘title‘=>‘hello test‘,‘content‘=>‘test how are you?‘,‘status‘=>0); 
$model = Mage::getModel(‘mynews/mynews‘)->load($id)->addData($data); 
try { 
   $model->save(); 
   echo "Data updated successfully."; 
} catch (Exception $e){ 
   echo $e->getMessage(); 
}

删除:

$id = 3; 
$model = Mage::getModel(‘mynews/mynews‘)->load($id); 
try { 
     $model->delete(); 
     echo "Data deleted successfully."; 
} catch (Exception $e){ 
     echo $e->getMessage(); 
}

magento CURD操作

原文:http://www.cnblogs.com/cangzhou/p/3868266.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!