首页 > 代码库 > magento CURD操作
magento CURD操作
查询:
$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操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。