Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Insert, Select, Update & Delete Data in Magento

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 467
    Comment on it

    Hello Readers,
    In this blog i am going to show you "How to select, insert, add, update/edit and delete data in the Magento.
    Lets Suppose, If you have database table named 'slider' with the following fields
    id(int,primay key), title(varchar), content(text) & status(smallint, value 0 or 1).
    & you have a module name 'Slider'. then, follow the below code to implement CRUD operations in magento.

     

    Select Data in Magento

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

    Here,
    item->getData() prints all data from ‘slider’ table.
    $item->getTitle() prints only one field i.e title from 'slider' table.

     

    Insert Data

    $data = array('title'=>'slide1','content'=>'this is my first slide','status'=>1);
    $model = Mage::getModel('slider/slider')->setData($data);
    $model->save();

    Here, $data contains array of data & 'title', 'content', 'status' is key of the array which should be database table’s field name.

     

    Update Data

    $id = 1;
    $data = array('title'=>'slide one','content'=>'this is first slide updated','status'=>1);
    $model = Mage::getModel('slider/slider')->load($id)->setData($data);
    $model->setId($id)->save();

    Here, $id is the database table id to be updated & $data contains array of data to be updated.

     

    Delete Data

    $id = 1; // $id is table id to be deleted.
    $model = Mage::getModel('slider/slider');
    $model->setId($id)->delete();

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: