Magento - 模块INSERT,UPDATE,DELETE,SELECT代码

时间:2013-03-13 02:29:19

标签: sql magento magento-1.7 crud

我创建了一个模块并希望使用核心写入和读取功能来插入,更新,删除或选择带条件的数据库值,如何在不使用SQL的情况下执行此操作? 例: $ CUSTOMER_ID = 123 模型=(引荐/引荐)

选择

 $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(有条件)= ???

5 个答案:

答案 0 :(得分:25)

假设我有一个名为mynews的模块。 以下是select, insert, update, and delete data表中news的代码。

INSERT DATA

$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

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

同样,要打印内容,我们需要编写$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是要更新的数据库表行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

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

通过这种方式,您可以在自定义模块和任何magento code中执行选择,插入,更新和删除。

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

答案 1 :(得分:2)

UPDATE基本上是SELECT和INSERT的组合。您加载一个集合,迭代它们根据需要设置值,然后在每个模型上调用 - > save()。

DELETE直接通过模型的 - > delete()功能来处理。因此,要么加载单个模型,要么迭代它们的SELECTed集合并调用 - > delete()

(不是因为迭代,这不是在集合上执行这些操作的'最快'方式(因为每个都将生成一个新查询,而不是一次处理多个删除的单个查询),但是对于小数据集/ SELECT(小于1k?)或者你不经常做的事情(比如每天导入或更新价格好的10k产品),性能都很好。

答案 2 :(得分:1)

FOR UPDATE

camparking <- structure(list(Ticket.Issue.Date = c("January 1, 2014", 
"January 2, 2014", 
"January 6, 2014", "March 9, 2014", "November 10, 2014", 
"November 30, 2014"
), Issue.Time = c("1:24AM", "10:45AM", "3:00PM", "3:29AM", "9:30PM", 
"10:20PM"), Location = c("Green ST", "Gore ST", "Franklin ST", 
"Green ST", "Marshall ST", "Erie ST"), Violation.Description = c("NO PARKING", 
"NO STOPPING", "HANDICAP RAMP", "NO PARKING", "METER PASSED", 
"METER PASSED")), .Names = c("Ticket.Issue.Date", "Issue.Time", 
"Location", "Violation.Description"), class = "data.frame",
 row.names = c(NA, -6L))

FOR DELETE

    $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";

答案 3 :(得分:0)

您也可以使用这样的选择查询。很容易。

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

答案 4 :(得分:0)

如果您想根据收集条件删除,可以使用addFieldToFilteraddAttributeToFilter

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