Suppose in magento database I have created a new table called ‘jyoti’ having the following fields.I have shown here how to select insert update and delete data in magento database using query concept.

id = integer
title = varchar
content = varchar
status = enum,

I have a module having the model called ‘jyoti’

so I want to do MySQL operation in this table by following operations.like INSERT,UPDATE,DELETE,SELECT.

You may like to explore Magento2 code from this article: How to insert select update and delete data in magento2

INSERT:

Here I am getting the value of title,content,status and I set all the values into an array called data like below.Also in magento structure. So.So I can do insert operation by save() method through my module ‘jyoti’.First I have to set the array value into my model then called the save method which will do the MySQL insert operation.

< ?php

$data = array(
    "title" => 'first testttile',
    "content" => "first testcontent",
    "status" => 0
);
$model = Mage::getModel('jyoti/jyoti')->setData($data);
try{
    $id = $model->save()->getId();
    echo "Data inserted successfully";
}catch(Exception $e){
    echo $e->getMessage();
}

?>

SELECT:

For select operation just simple do call getCollection() method in the model then surely you will get object variable of collection.So by moving around through foreach loop you will get all the data one by one.You can call your desire data with respect to column name.for example : I have title in my table ‘jyoti’.I have got the object of that table.I Can get title by simple calling getTitle() method.

That is $collection->getTitle();.Like ways

$collection->getContent();
$collection->getStatus();
$collection->getId();

< ?php

$model = Mage::getModel('jyoti/jyoti');
$collections = $model->getCollection();
foreach($collection as $collection){
    print_r($collection->getdata());
}

?>

UPDATE:

Suppose I am getting id from request and I want to update the details related to that id in database table.

< ?php

$id = $this->getRequest()->getParams('id');
//$id = 1;
$data = array(
    "title" => 'testtitle',
    "content" => "testcontent",
    "status" => 1
);
$model = Mage::getModel('jyoti/jyoti')->load($id)->addData($data);
try{
    $model->setId($id)->save();
    echo "Data updated successfully";
}catch(Exception $e){
    echo $e->getMessage();
}

?>

or

If I want to update single data taht is I have title as testtitle and I want that as newstesttitle in my database then I can use the following method.

< ?php

$model = Mage::getModel('jyoti/jyoti')->load($id);
$model->addData('testcontent','newtestconent');
try{
    $model->setId($id)->save();
    echo "Data updated successfully";
}catch(Exception $e){
    echo $e->getMessage();
}

?>

DELETE:

I have id on above request.I can simple delete related data from table ‘jyoti’.So please follow below process to do the delete operation.

< ?php

$id = $this->getRequest()->getParams('id');
//$id = 1;
$model = Mage::getModel('jyoti/jyoti');
try{
    $model->setId($id)->delete();
    echo "Data deleted successfully";
}catch(Exception $e){
    echo $e->getMessage();
}

?>

Hope you enjoyed the article and don’t forget to share it.

How to select insert update and delete data in magento database
Tagged on:     

3 thoughts on “How to select insert update and delete data in magento database

  • September 5, 2014 at 3:10 am
    Permalink

    Great article .Keep it up

    Reply
  • September 1, 2015 at 4:28 am
    Permalink

    Good Morning Sir,

     

    I am beginner of Magento.  So please help me how to begin to learn magento. if any reference please send me link.

    Thank you..

    Reply
  • September 1, 2015 at 4:31 am
    Permalink

    How to create module in magento..for this operation

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *