Tuesday, October 1, 2013

SELECT,INSERT,UPDATE,AND DELETE DATA (CRUD) IN MAGENTO

I have a database table named “test” with the following fields given below:

SELECT DATA

$model = Mage::getModel('test/test');
$collection = $model->getCollection();
foreach($collection as $item){
    print_r($item->getData());
    print_r($item->getTitle());
}

INSERT DATA

$data = array('title'=>'hello EWA','content'=>'This is for the Testing on the Insert data','status'=>1);
$model = Mage::getModel('mytestimonial/mytestimonial')->setData($data);
try {

       $insertId = $model->save()->getId();
        echo "Data has been saved successfully. Insert ID: ".$insertId;
    } catch (Exception $e){
     echo $e->getMessage();
}

UPDATE DATA

// $sid = $this->getRequest()->getParam('id');
$sid = 5;
$data = array('title'=>'hello EWA','content'=>'This is for the Testing on the Update data','status'=>0);
$model = Mage::getModel('mytestimonial/mytestimonial')->load($sid)->addData($data);
try {
        $model->setId($sid)->save();
        echo "Data has been updated successfully.";
       
    } catch (Exception $e){
        echo $e->getMessage();
}

DELETE DATA

$sid is the database table row id to be deleted.

// $sid = $this->getRequest()->getParam('id');
$sid = 5;
$model = Mage::getModel('mytestimonial/mytestimonial');
try {
        $model->setId($sid)->delete();
        echo "Data deleted successfully.";
       
    } catch (Exception $e){
        echo $e->getMessage();
}

No comments:

Post a Comment

Please mention your comments.......