magento2 数据库查询
来源:互联网 发布:qq飞车黄金雷诺数据 编辑:程序博客网 时间:2024/05/20 14:20
调试数据集 Select
$collection = $this->_objectManager->get( 'Magento\Catalog\Model\ProductFactory' )->create()->getCollection();echo $collection->load()->getSelectSql( true );
通过 Select 获取数据
$conn = $this->_objectManager->get( 'Magento\Framework\App\ResourceConnection' )->getConnection();$tblMain = $conn->getTableName( 'cms_page' );$tblStore = $conn->getTableName( 'cms_page_store' );$select = $conn->select() ->distinct() ->from( [ 'page' => $tblMain ], [ 'page_id', 't' => 'title' ] ) ->join( [ 'store' => $tblStore ], 'store.page_id = page.page_id', [ 'sid' => 'store_id' ] ) ->where( 'is_active = ?', '1' ) ->order( 'title ASC' ) ->limit( 5, 1 );$data = $conn->fetchAll( $select );
聚合查询(SUM)
$conn = $this->_objectManager->get( 'Magento\Framework\App\ResourceConnection' )->getConnection();$tbl = $conn->getTableName( 'sales_order_item' );// 所有记录总计$select = $conn->select() ->from( $tbl, [ 'total' => new \Zend_Db_Expr( 'SUM( qty_ordered )' ) ] ) ->where( 'order_id = ?', '1' );$result = $conn->fetchOne( $select );// 各局部统计$select = $conn->select() ->from( $tbl, [ 'order_id', 'total' => new \Zend_Db_Expr( 'SUM( qty_ordered )' ) ] ) ->group( 'order_id' ) ->having( 'order_id != ?', '1' );$result = $conn->fetchAll( $select );
更新数据
/** @var $conn \Magento\Framework\App\ResourceConnection *//** @var $storeId int *//** @var $ids array */$tbl = $conn->getTableName( 'sales_order_item' );// 插入数据$conn->insert( $tbl, [ 'field_one' => $fieldOne, 'field_two' => $fieldTwo, 'field_three' => $fieldThree ] );// 插入数据,碰到已存在的记录(primary / unique)则只更新指定的字段$conn->insertOnDuplicate( $tbl, [ 'field_one' => $fieldOne, 'field_two' => $fieldTwo, 'field_three' => $fieldThree ], [ 'field_three' ] );// 更新数据$conn->update( $tbl, [ 'field_one' => $fieldOne, 'field_two' => $fieldTwo ], $conn->quoteInto( 'store_id IN (?)', $storeId ) );// 删除数据$conn->delete( $tbl, [ 'id IN (?)' => $ids ] );
Model
/* @var \Magento\Directory\Model\ResourceModel\Region\Collection $collection */$collection = $objectManager->get('Magento\Directory\Model\ResourceModel\Region\Collection');$collection->getSelect() ->where('main_table.region_id = ?', 1) ->where('main_table.country_id=?', 'US');foreach($collection as $row) { echo $row->getData('default_name');}
阅读全文
0 0
- magento2 数据库查询
- magento2 发布
- magento2 email
- 6. Magento2 --- 创建主题
- Magento2 常用代码
- Magento2--创建主题
- magento2 入门引导
- Magento2 文件目录结构
- magento2 重置后台密码
- magento2 命令行插件安装
- magento2 如何卸载模块
- magento2 命令行安装
- Magento2开发文档教程
- Magento2开发文档教程
- Magento2开发教程
- Magento2开发教程
- Magento2开发教程
- Magento2开发教程
- 下载时,如何选择低版本的mysql的编译安装包,比如如何下载:mysql-5.7.14.tar.gz
- mac上用Parallels安装CentOS(自定义分区、图形化界面)
- c语言实现简单的猜数字游戏
- VMware利用vmtools 实现Centos和windows 共享文件夹
- 面试题(判断元素出栈入栈顺序的合法性。)
- magento2 数据库查询
- SQL优化之索引
- Spring中quartz任务调度执行两次的问题处理
- 调戏京东客服必备:教你查询京东客服MM的真实姓名
- js array.filter实例(数组去重)
- jq 替换数组中某个元素
- java操作符
- 美拍、秒拍短视频SDK功能实现
- Android经典例子ViewPager、动态换肤、Tab标签效果、Renderscript、移动地图+选址、主页滑动效果、侧滑菜单沉浸式状态栏