Magento model

某个字段保存不了 entity/customer _getDefaultAttributes添加字段名

Java代码   收藏代码
  1. $customer = Mage::getModel('customer/customer')->load(1);      
  2. $customer->setData('is_charge''2');  
  3. $customer->save(); //is_charge保存不成功原因  

对某个字段进行算法操作或函数操作用new Zend_Db_Expr

Java代码   收藏代码
  1. array('point'=>new Zend_Db_Expr('pd.value*2'))  

model/customer/customer.php

Java代码   收藏代码
  1. Mage::getSingleton('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail('demo@demo.com');  

Model的启用,可以用Model下的文件 etc/config.xml

Java代码   收藏代码
  1. <models>  
  2.     <ticket>  
  3.         <class>Test_Ticket_Model</class>  
  4.         <resourceModel>ticket_mysql4</resourceModel>  
  5.     </ticket>  
  6. </models>  

CURD操作:

Java代码   收藏代码
  1. public function createNewPostAction() {   
  2.     $blogpost = Mage::getModel('ticket/log');   
  3.     $blogpost->setTitle('Code Post!');   
  4.     $blogpost->setPost('This post was created from code!');   
  5.     $blogpost->save();   
  6.     echo 'post created';   
  7. }  
  8.   
  9. public function editFirstPostAction() {   
  10.     $blogpost = Mage::getModel('ticket/log');   
  11.     $blogpost->load(1); //load($id, $field=null)   $filed = '键值'  
  12.     $blogpost->setTitle("The First post!");   
  13.     $blogpost->save();   
  14.     echo 'post edited';   
  15. }  
  16.   
  17. public function deleteFirstPostAction() {   
  18.     $blogpost = Mage::getModel('ticket/log');   
  19.     $blogpost->load(1);   
  20.     $blogpost->delete();   
  21.     echo 'post removed';   
  22. }  
  23. //查询(select)语句  
  24. $connection = Mage::getSingleton('core/resource')->getConnection('core_read');  
  25. $select = "select * from table;"  
  26. //$select = $connection->select()->from('table', array('*')) ;  
  27. $rowsArray = $connection->fetchOne($select); // return row index0  
  28. $rowArray =$connection->fetchRow($select);   //return row  
  29.   
  30. //插入(insert)语句  
  31. $fields = array();  
  32. $fields['name']= 'test';  
  33. $connection->insert('tablename', $fields);  
  34.   
  35. //更新(update)语句  
  36. $connection->beginTransaction();  
  37. $fields = array();  
  38. $fields['name'] = 'jony';  
  39. $where = $connection->quoteInto('id =?''1');  
  40. $connection->update('tablename', $fields, $where);  
  41. $connection->commit();  
  42.   
  43. //删除(delete)语句  
  44. $condition = array($connection->quoteInto('id=?''1'));  
  45. $connection->delete('tablename', $condition);  

注意上面的getConnection()方法中的参数 "core_read",表明了Magento将要使用的资源。与之相对应,当我们修改数据库的时候使用参数"core_write".一般情况下 getConnection方法的参数应设成"core_read" 或 "core_write"(应该不指定也是可以的,但是如果Magento有多个数据库就必须指定了 )。对应上面新增的module的名字.使用下面相对应的语句在read或write Database:

Java代码   收藏代码
  1. $conn = Mage::getSingleton('core/resource')->getConnection('ticket_read');  
  2. $conn = Mage::getSingleton('core/resource')->getConnection('ticket_write');  

local\Test\Ticket\etc\config.xml  model添加资源模型和实体对象,可以用Model\Mysql4下的文件

Java代码   收藏代码
  1. <global>  
  2.     <models>  
  3.         <ticket>  
  4.             <class>Test_Ticket_Model</class>  
  5.             <resourceModel>ticket_mysql4</resourceModel>  
  6.         </ticket>  
  7.         <ticket_mysql4>  
  8.             <class>Test_Ticket_Model_Mysql4</class>  
  9.             <entities>  
  10.                 <log><!--model name-->  
  11.                     <table>ticket_log</table><!-- table name-->  
  12.                 </log>  
  13.                 <type>  
  14.                     <table>ticket_type</table>  
  15.                 </type>  
  16.                 <ticket>  
  17.                     <table>ticket</table>  
  18.                 </ticket>  
  19.             </entities>  
  20.         </ticket_mysql4>  
  21.     </models>  
  22.    <resources>  
  23.         <ticket_write>  
  24.             <connection>  
  25.                 <use>activity_setup</use>  <!-- config  app\etc\config.xml-->  
  26.             </connection>  
  27.         </ticket_write>  
  28.         <ticket_read>  
  29.             <connection>  
  30.                 <use>activity_setup</use>  
  31.             </connection>  
  32.         </ticket_read>  
  33.     </resources>  
  34. </global>  

app\etc\config.xml

Java代码   收藏代码
  1. <resources>  
  2. ****  
  3.     </core_read>  
  4.   
  5.     <activity_setup><!-- new -->  
  6.         <connection>  
  7.             <host>localhost</host>  
  8.             <username>root</username>  
  9.             <password>Test</password>  
  10.             <dbname>Test_activity</dbname>  
  11.             <model>mysql4</model>  
  12.             <initStatements>SET NAMES utf8</initStatements>  
  13.             <type>pdo_mysql</type>  
  14.             <active>1</active><!-- 是1 -->  
  15.         </connection>  
  16.     </activity_setup>  
  17. </resources>  
  18. <resource>  
  19.     <connection>  
  20.         <types>  
  21.             <pdo_mysql>  
  22.                 <class>Mage_Core_Model_Resource_Type_Db_Pdo_Mysql</class>  
  23.             </pdo_mysql>  
  24.         </types>  
  25.     </connection>  
  26. </resource>  

local\Test\Ticket\Model\Log.php

Java代码   收藏代码
  1. <?php  
  2. class Test_Ticket_Model_Log extends Mage_Core_Model_Abstract  
  3. {  
  4.     public function _construct()  
  5.     {  
  6.         parent::_construct();  
  7.         $this->_init('ticket/log');  
  8.     }  
  9.   
  10.     public function saveInfo($rewriteData)  
  11.     {  
  12.         return $this->getResource()->saveWeiboData($rewriteData);  
  13.     }  
  14.   
  15.     public function getLastRechargeReal($customerId, $rechargeType) //array  
  16.     {  
  17.         return $this->getResource()->getLastRechargeReal($customerId, $rechargeType);  
  18.     }  
  19.     public function getRecommendProducts()  //object  
  20.     {  
  21.         if (!$this->getId()) {  
  22.             return array();  
  23.         }  
  24.       
  25.         $array = $this->getData('recommend_products');  
  26.         if (is_null($array)) {  
  27.             $array = $this->getResource()->getRecommendProducts($this);  
  28.             $this->setData('recommend_products', $array);  
  29.         }  
  30.         return $array;  
  31.     }  
  32.     public function saveCardInfo($cardNo)  
  33.     {  
  34.         try {  
  35.             /*开始事务*/  
  36.             $this->getResource()->beginTransaction();  
  37.   
  38.             $customer = Mage::getSingleton('customer/session')->getCustomer();  
  39.             $oldcard = $customer->getData('idcard');  
  40.             #更新卡状态为绑定  
  41.             $idcard = $this->loadByCardno($cardNo);  
  42.             $idcard->setData('idcard_status', self::IDCARD_STATUS_BINDING);  
  43.             $idcard->save();  
  44.   
  45.             #更新用户的ID卡  
  46.             $customer->setData('idcard', $cardNo);  
  47.             $customer->save();  
  48.   
  49.             $this->getResource()->commit();  
  50.         } catch (exception $e) {  
  51.             $this->getResource()->rollBack();  
  52.         }  
  53.     }  
  54. }  

Mysql4  local\Test\Ticket\Model\Mysql4\Log.php

Java代码   收藏代码
  1. <?php  
  2. class Test_Ticket_Model_Mysql4_Log extends Mage_Core_Model_Mysql4_Abstract {  
  3.     public function _construct() {  
  4.         //$this->_setResource(array('read' =>'ticket_read', 'write' =>'ticket_write'));//多数据库  
  5.         $this->_init('ticket/log''id'); #主键  
  6.     }  
  7.   
  8.     public function getLastRechargeReal($customerId, $rechargeType) {  
  9.         $sql = $this->_getReadAdapter()->select()->from($this->getMainTable(), array('customer_id'))  
  10.             ->where('customer_id = ?', $customerId)  
  11.             ->where('recharge_type = ?', $rechargeType)  
  12.             ->order(array('id DESC'))  
  13.             ->limit(1);  
  14.   
  15.         return $this->_getReadAdapter()->fetchRow($sql);  
  16.     }  
  17.   
  18.     //联表查询  
  19.     public function getAttributesUsedInListing() {  
  20.         $sql = $this->_getReadAdapter()->select()  
  21.             ->from(array('main_table' => $this->getMainTable()))  
  22.             ->join(array('additional_table' => $this->getTable('catalog/eav_attribute')),  
  23.             'main_table.attribute_id = additional_table.attribute_id',  
  24.             array())  
  25.             ->joinLeft(array('al' => $this->getTable('eav/attribute_label')),  
  26.             'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int)$this->getStoreId(),  
  27.             array('store_label' => new Zend_Db_Expr('IFNULL(al.value, main_table.frontend_label)')))  
  28.             ->where('additional_table.used_in_product_listing=?'1);  
  29.   
  30.         // $sql = $sql->assemble();  
  31.         // echo $sql;  
  32.         return $this->_getReadAdapter()->fetchAll($sql);  
  33.     }  
  34.   
  35.     public function saveInfo($rewriteData) {  
  36.         $this->_getWriteAdapter()->insert($this->getMainTable(), $rewriteData);  
  37.         return $this->_getWriteAdapter()->lastInsertId();  
  38.     }  
  39.   
  40.     public function updateInfo($id, $rewriteData) {  
  41.         $this->_getWriteAdapter()->update($this->getMainTable(), $rewriteData, $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . '=?', $id));  
  42.     }  
  43.   
  44.     public function test() {  
  45.         $sql = 'update ' . $this->getMainTable() . " set a=1 where id=1";  
  46.         return $this->_getReadAdapter()->query($sql);  
  47.         //$this->beginTransaction();  
  48.  $this->_getWriteAdapter()->delete($this->getMainTable(), array("email='$email''""type='$type'"));  
  49.         //$this->_getWriteAdapter()->delete($this->getMainTable(), $this->_getWriteAdapter()->quoteInto($this->getIdFieldName().'=?', $id));  
  50.     }  
  51.   
  52.     public function count() {  
  53.         $table = $this->getMainTable();  
  54.         $select = $this->_getReadAdapter()  
  55.             ->select()  
  56.             ->from($table)  
  57.             ->reset('columns')  
  58.             ->columns(new Zend_Db_Expr('count(*)'));  
  59.         echo $select . '<br>';  
  60.   
  61.         $select = $this->_getReadAdapter()  
  62.             ->select()  
  63.             ->from($table)  
  64.             ->reset('columns')  
  65.             ->columns(new Zend_Db_Expr('max(list_id)'));  
  66.         echo $select . '<br>';  
  67.     }  
  68. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值