zoukankan      html  css  js  c++  java
  • magento2 数据库查询

    调试数据集 Select

    $collection = $this->_objectManager->get( 'MagentoCatalogModelProductFactory' )->create()->getCollection();
    echo $collection->load()->getSelectSql( true );

    通过 Select 获取数据

    $conn = $this->_objectManager->get( 'MagentoFrameworkAppResourceConnection' )->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( 'MagentoFrameworkAppResourceConnection' )->getConnection();
    $tbl = $conn->getTableName( 'sales_order_item' );

    // 所有记录总计
    $select = $conn->select()
    ->from( $tbl, [ 'total' => new end_Db_Expr( 'SUM( qty_ordered )' ) ] )
    ->where( 'order_id = ?', '1' );
    $result = $conn->fetchOne( $select );

    // 各局部统计
    $select = $conn->select()
    ->from( $tbl, [ 'order_id', 'total' => new end_Db_Expr( 'SUM( qty_ordered )' ) ] )
    ->group( 'order_id' )
    ->having( 'order_id != ?', '1' );
    $result = $conn->fetchAll( $select );

    更新数据

     

    /** @var $conn MagentoFrameworkAppResourceConnection */
    /** @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 MagentoDirectoryModelResourceModelRegionCollection $collection */
    $collection = $objectManager->get('MagentoDirectoryModelResourceModelRegionCollection');
    $collection->getSelect()
    ->where('main_table.region_id = ?', 1)
    ->where('main_table.country_id=?', 'US');
    foreach($collection as $row) {
    echo $row->getData('default_name');
    }

    原文链接:https://blog.csdn.net/joe_develope/article/details/78094478

  • 相关阅读:
    虚拟化技术一些概念整理
    更改KVM虚拟机root的密码
    文件作为块设备访问
    KVM虚拟机IO处理过程(二) ----QEMU/KVM I/O 处理过程
    KVM虚拟机IO处理过程(一) ----Guest VM I/O 处理过程
    KVM的初始化过程
    linux删除文件未释放空间问题处理
    cgroup测试存储设备IOPS分配
    数组中的逆序对
    VMware Tools安装方法及解决无法全屏显示问题
  • 原文地址:https://www.cnblogs.com/focai/p/11726191.html
Copyright © 2011-2022 走看看