magento CURD操作
生活随笔
收集整理的這篇文章主要介紹了
magento CURD操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
查詢:
$model = Mage::getModel('mynews/mynews');
$collection = $model->getCollection();
$collection->addAttributeToSelect(array('name', 'image', 'url_key')) //選擇要select的屬性
->setPageSize(20)//獲取的個數(shù)
->setCurPage(1)
->addAttributeToFilter('style', 'brass');//屬性過濾 詳細(xì)文檔 http://justcoding.iteye.com/blog/1681318
->addAttributeToSort('name', 'ASC');//排序
$collection->getSize();
foreach($collection as $item){
print_r($item->getData());
print_r($item->getTitle());
}
創(chuàng)建:
$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();
}
總結(jié)
以上是生活随笔為你收集整理的magento CURD操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: acrobat 导出300dpi图片
- 下一篇: Postman入门