SQL Magento - 模块插入、更新、删除、选择代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15375986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento - Module INSERT,UPDATE, DELETE, SELECT code
提问by Lim Heng Cheun
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)
我创建了一个模块,想使用核心读写功能来插入、更新、删除或选择有条件的数据库值,不使用 SQL 怎么办?示例:$customer_id=123 模型=(推荐/推荐)
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)=???
删除,更新(有条件)=???
回答by Dead Man
Suppose, I have a module named mynews
.
Here follows the code to select, insert, update, and delete data
from the news
table.
假设,我有一个名为mynews
. 这里如下的代码select, insert, update, and delete data
从news
表中。
INSERT DATA
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
包含要插入的数据数组。数组的键应该是数据库表的字段名,值应该是要插入的值。
$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
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()
.
同样,要打印内容,我们需要编写$item->getContent()
.
$model = Mage::getModel('mynews/mynews');
$collection = $model->getCollection();
foreach($collection as $item){
print_r($item->getData());
print_r($item->getTitle());
}
UPDATE DATA
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
是要更新的数据库表行 ID。
$data
包含要更新的数据数组。数组的键应该是数据库表的字段名,值应该是要更新的值。
// $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
DELETE DATA
$id
is the database table row id to be deleted.
$id
是要删除的数据库表行ID。
// $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
.
通过这种方式,您可以在自定义模块和任何magento code
.
Source: http://blog.chapagain.com.np/magento-how-to-select-insert-update-and-delete-data/
来源:http: //blog.chapagain.com.np/magento-how-to-select-insert-update-and-delete-data/
回答by Jared Kipe
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.
UPDATE 基本上是 SELECT 和 INSERT 的组合。您加载一个集合,根据需要迭代它们设置值,然后在每个模型上调用 ->save()。
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()
DELETE 是通过模型的 ->delete() 函数直接处理的。因此,要么加载单个模型,要么遍历它们的 SELECTed 集合并调用 ->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).
(并不是由于迭代,这不是对集合执行这些操作的“最快”方式(因为每个都将生成一个新查询,而不是一次处理多个删除的单个查询),但是性能适用于小数据集/SELECT(小于 1k?)或您不经常做的事情(例如每天一次导入或更新 10k 产品的价格)。
回答by Ashwin Shahi
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.";
回答by sanji
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);
回答by Rahul Dadhich
If you want to delete with condition based on collection you can use addFieldToFilter
, addAttributeToFilter
如果要根据集合删除条件,可以使用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();
}