首页 > 代码库 > 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操作