Magento 开发系列文章
操作数据库
Magento连接数据库执行查询
1 2 3 4 5 6 7 8 9 10 11 |
<?php $connection = Mage::getSingleton('core/resource')->getConnection('core_read'); $select = $connection->select() ->from('catalog_product_entity', array('*')) // 选择查询所有字段,如果只想查询一个字段,把*改成字段名 ->where('entity_id=?',1); // where id =1 //->group('name'); // group by name $rowsArray = $connection->fetchAll($select); // 返回所有行 $rowArray =$connection->fetchRow($select); //返回行 var_dump($rowArray); echo $rowArray[sku]; ?> |
执行时需要include或require一下app/Mage.php文件,若出现类似”Fatal error: Call to a member function getModelInstance() on a non-object in D:\xampp\htdocs\magento\app\Mage.php on line 463“报错,请尝试添加一句
1 |
$app = Mage::app('default'); |
Magento对数据库执行增删改查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
以下直接提供让magento操作支持直接操作数据库修改方法, 查: $read = Mage::getSingleton("core/resource”)->getConnection('core_read'); $sql = "select * from `abc`”; $result = $read->fetchAll($sql); //fetchRow查找一条 增,删,改 $write = Mage::getSingleton("core/resource")->getConnection('core_write'); $sql = "insert into abc (name)values('hello')”; $write->query($sql); 另一种写法: 增 $write = Mage::getSingleton("core/resource”)->getConnection('core_write'); $table = Mage::getSingleton('core/resource')->getTableName('abc'); $write->insert($table,array('name'=>'hello')); 改 $write = Mage::getSingleton("core/resource”)->getConnection('core_write'); $table = Mage::getSingleton('core/resource')->getTableName('abc'); $write->update($table,array('name'=>'abc'),array('id=?'=>3)); 删 $write = Mage::getSingleton("core/resource”)->getConnection('core_write'); $table = Mage::getSingleton('core/resource')->getTableName('abc'); $write->delete($table,array('id=?'=>3)); 查 $read = Mage::getSingleton("core/resource”)->getConnection('core_read'); $table = Mage::getSingleton('core/resource')->getTableName('abc'); $result = $read->select()->from(array('main_table'=>$table))->where('main_table.id=?',3)->limit(1); $products=$read->fetchAll($result); |
判断用户登录状态
1 2 3 4 5 |
Mage::getSingleton( 'customer/session' )->isLoggedIn(); //已登录可获取客户相关信息,如姓名,email $customer = Mage::getSingleton('customer/session')->getCustomer(); echo $customer->getName(); echo $customer->getEmail(); |
CMS代码
邮件订阅
1 |
{{block type="newsletter/subscribe" template="newsletter/subscribe.phtml"}} |
自定义代码
1 |
{{block type="core/template" name="mycode" template="mycode/myfile.phtml"}} |