13

I created a module and want to used core write and read function to insert,update,delete or select database value with condition, how can I do it without using SQL? Example: $customer_id=123 Model=(referral/referral)

SELECT

 $collection3 = Mage::getModel('referral/referral')->getCollection();
    $collection3->addFieldToFilter('customer_id', array('eq' => $customer_id));
    foreach($collection3 as $data1)
    {
    $ref_cust_id.= $data1->getData('referral_customer_id'); 
    }

INSERT

$collection1= Mage::getModel('referral/referral');
$collection1->setData('customer_id',$customer_id)->save();

DELETE,UPDATE(with condition)=???

tereško
  • 58,060
  • 25
  • 98
  • 150
Lim Heng Cheun
  • 229
  • 1
  • 3
  • 13

5 Answers5

25

Suppose, I have a module named mynews. Here follows the code to select, insert, update, and delete data from the news table.

INSERT DATA

$data contains array of data to be inserted. The key of the array should be the database table’s field name and the value should be the value to be inserted.

$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();   
}

SELECT DATA

$item->getData() prints array of data from ‘news’ table.
$item->getTitle() prints the only the title field.

Similarly, to print content, we need to write $item->getContent().

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

UPDATE DATA

$id is the database table row id to be updated. $data contains array of data to be updated. The key of the array should be the database table’s field name and the value should be the value to be updated.

// $id = $this->getRequest()->getParam('id');
$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->setId($id)->save();
    echo "Data updated successfully.";

} catch (Exception $e){
    echo $e->getMessage(); 
}

DELETE DATA

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

// $id = $this->getRequest()->getParam('id');
$id = 3;
$model = Mage::getModel('mynews/mynews');
try {
    $model->setId($id)->delete();
    echo "Data deleted successfully.";

} catch (Exception $e){
    echo $e->getMessage(); 
}

In this way you can perform select, insert, update and delete in your custom module and in any magento code.

Source: http://blog.chapagain.com.np/magento-how-to-select-insert-update-and-delete-data/

Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120
Dead Man
  • 2,880
  • 23
  • 37
2

UPDATE is basically the combination of SELECT and INSERT. You load a collection, iterate over them setting the values as needed, then call ->save() on each model.

DELETE is handled directly via the ->delete() functon of models. So either load a single model or iterate over a SELECTed collection of them and call ->delete()

(Not that due to the iteration, this is not the 'fastest' way of doing these operations on collections (because each one is going to generate a new query, instead of a single query that handles multiple deletes at once), but the performance is fine for either small data sets/SELECTs (less than 1k?) or for things that you don't do very often (like importing or updating prices ok 10k products once per day).

Jared Kipe
  • 1,189
  • 6
  • 5
1

FOR UPDATE

    $new=$this->getRequest()->getParams();
    $id=$new['id'];
    $name=$new['name'];

    $con=Mage::getModel('plugin/plugin')->load($id);
      $con->setData('name',$name)->save();

    echo "Update Success";

FOR DELETE

    $id = $this->getRequest()->getParam('id');
    $model = Mage::getModel('plugin/plugin');
    $model->setId($id)->delete();
    echo "Data deleted successfully.";
Ashwin Shahi
  • 339
  • 2
  • 14
0

You can use select query like this also. its very easy.

$salesInvoiceCollection_sql = "SELECT  `entity_id` ,  `increment_id`,`order_id` 
                                    FROM  `sales_flat_invoice` 
                                    WHERE  `erp_invoice_id` = 0
                                    ORDER BY `entity_id`
                                    DESC limit 1000";

    $salesInvoiceCollection = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchAll($salesInvoiceCollection_sql);
sanji
  • 1,310
  • 1
  • 12
  • 21
0

If you want to delete with condition based on collection you can use addFieldToFilter, addAttributeToFilter

$model = Mage::getModel('mynews/mynews')->getCollection();
try {
    $model->addAttributeToFilter('status', array('eq' => 1));
    $model->walk('delete');
    echo "Data deleted successfully.";

} catch (Exception $e){
    echo $e->getMessage(); 
}
Rahul Dadhich
  • 1,213
  • 19
  • 32