$collection = $this->_objectManager->get( 'Magento\Catalog\Model\ProductFactory' )->create()->getCollection(); echo $collection->load()->getSelectSql( true );
$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 );
$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 ] );
/* @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'); }
http://www.webmull.com/magent...php
$collection->addAttributeToFilter($field, [ 'in' => $arr ]); $collection->addAttributeToSort($field, 'desc'); $collection->addStoreFilter(); $collection->addUrlRewrite(); $collection->setPageSize( 10 ); $collection->addAttributeToSelect($field);
Entity collection有查詢field的能力,只要查詢條件裏跟field有關,就會自動把field所在的表join到查詢中web
$collection->addAttributeToSelect($field, 'left'); // 等同於select xx as field ... left join xxx $collection->addAttributeToFilter($field, 'field > 0')// 等同於where field > 0 $collection->setOrder($field, 'ASC'); // 等同於order by xxx ASC
special price是個特別的個案,特價須要在有效期間生效,有效期外不生效fetch
$collection->addAttributeToSelect('special_from_date', 'left'); $collection->addAttributeToSelect('special_to_date', 'left'); $collection->addAttributeToSelect('special_price', 'left'); $collection->getSelect()->order('IF(special_from_date < now() AND special_to_date > now(), special_price, 0) DESC');