Magento model

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

某个字段保存不了 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. }  
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
12月前
|
SQL 存储 缓存
Django model 层之Making Query总结2
Django model 层之Making Query总结
81 0
|
12月前
|
SQL 关系型数据库 MySQL
Django model 层之Making Query总结1
Django model 层之Making Query总结
52 0
|
12月前
|
存储 网络协议 关系型数据库
Django model 层之Models与Mysql数据库小结1
Django model 层之Models与Mysql数据库小结
77 0
|
12月前
|
存储 SQL 网络协议
Django model 层之Models与Mysql数据库小结2
Django model 层之Models与Mysql数据库小结
103 0
|
12月前
|
存储 数据可视化 数据库
odoo Actions学习总结
odoo Actions学习总结
84 0
Antd Model完全自定义
Antd Model完全自定义
242 0
Antd Model完全自定义
|
Python
Django BBS项目 models.py 文件
Django BBS项目 models.py 文件
|
TensorFlow 算法框架/工具
tensorflow报错:AttributeError: module ‘tensorflow._api.v2.compat.v1‘ has no attribute ‘Sessions‘,亲测有效
tensorflow报错:AttributeError: module ‘tensorflow._api.v2.compat.v1‘ has no attribute ‘Sessions‘,
1419 0
tensorflow报错:AttributeError: module ‘tensorflow._api.v2.compat.v1‘ has no attribute ‘Sessions‘,亲测有效
|
Python
Django 2.0 model on_delete错误小记
1、前言 今天用 Django 2.0 时模型的外键报了一个错误: TypeError: __init__() missing 1 required positional argument: 'on_delete' 2、原因 经过筛查,在创建多对一的关系的,需要在Foreign的第二参数中加入 on_delete=models.CASCADE 主外关系键中,级联删除,也就是当删除主表的数据时候从表中的数据也随着一起删除。
1260 0
|
数据库 Python
Django setting.py添加app
在django创建app中,修改models.py添加对应的数据库表,后执行 makemigrations Mynewsite 提示: App Mynewsite could not be found.
1422 0