Magento

Magento 直接使用sql:query for create, read, update and delete

Magento database MySQL read and write adapters.

//database read adapter 
$read = Mage::getSingleton('core/resource')->getConnection('core_read'); 
 
//database write adapter 
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
 
//Functions of read and write adapter
$readMethods = get_class_methods(get_class($read));foreach($readMethods as $r){echo $r."\n";}
$writeMethods = get_class_methods(get_class($write));foreach($writeMethods as $r){echo $r."\n";}

Magento Read Query, returns an array.(查询)

$results = $read->fetchAll("select * from customer_entity where email like '%@codexpedia.com'"); 
var_dump($results[0]);
foreach($results as $r)
{
    var_dump($r);
}

Magento Insert Query. (插入,增加数据)

$write->insert(
        "A_TABLE_NANE", 
        array("a_column_name" => value, "a_column_name" => value)
);

注意这里的value值不能是变量。

Magento Update Query.(更新 ,修改数据)

$write->update(
        "A_TABLE_NANE",
        array("a_column_name" => value, "a_column_name" => value),
        "entity_id=18"
);

Magento Delete Query.(删除数据)

$write->delete(
    "A_TABLE_NANE",
    "entity_id=18"
);

Magento Raw Query. It can be used for create, read, update, and delete operations.(查询)

//raw query Varien_Db_Statement_Pdo_Mysql
$customers = $read->query("select * from customer_entity");
var_dump($customers);
foreach($customers as $c)
{
    var_dump($c);
}

参考文档资料:
https://www.codexpedia.com/magento/magento-direct-mysql-query-for-create-read-update-and-delete/

一路走来,有你。

Write A Comment