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

  • 相关阅读:
    vue删除表格内的数据后局部刷新页面
    git到GitHub的操作和遇到的一些问题
    git push失败
    导入小程序错误
    WebStorm安装
    Office安装时报错1907的解决方法
    转战物联网·基础篇11-物联网架构与互联网及普通硬件项目的本质差异及重点概述
    转战物联网·基础篇10-物联网架构硬件端的特点及行业应用,对初创项目的选型建议
    Windows系统Git配置教程(Git配置git config)
    Windows7安装PowerShell5.1方法(Flutter新版本需要)
  • 原文地址:https://www.cnblogs.com/focai/p/11726191.html
Copyright © 2011-2022 走看看