在$ this-> setCollection($ collection)之前设置记录计数;

时间:2019-05-14 10:36:20

标签: grid report magento-1.9 php-7.2 magento-collection

在设置网格之前,如何设置集合大小的计数。

主要销售集合:
    $ collection = Mage :: getModel('sales / order')-> getCollection()

            ->addAttributeToFilter('created_at', array('gteq' =>$fromDate))
            ->addAttributeToFilter('created_at', array('lteq' =>$toDate))
            ->addAttributeToFilter('status',array('eq'=>Mage_Sales_Model_Order::STATE_COMPLETE));

已加入该收藏集:

$Table = Mage::getModel('amorderattr/attribute')->getResource()->getTable('amorderattr/order_attribute');
        $collection->getSelect()->joinLeft(array('custom_attributes' => $amastyTable), 'main_table.entity_id = custom_attributes.order_id',array('is_first_order'));

已加载另一个收藏集:

$CustomersCollection = Mage::getModel('customer/customer')->getCollection();
        $newCustomersCollection = $CustomersCollection
            ->addAttributeToFilter('created_at',array('from'=>$fromDate,'to'=>$toDate));
        if(strlen($storeId)==1)
            $newCustomersCollection->addFieldToFilter('website_id',array('eq'=>Mage::getModel('core/store')->load($storeId)->getWebsiteId()));
        else if($storeArr){
            $newCustomersCollection->addFieldToFilter('website_id',array('eq'=>Mage::getModel('core/store')->load($storeArr[0])->getWebsiteId()));
        }
        $newCustomers = count($newCustomersCollection);

时间段:

$frm = date("d M Y", strtotime($filterData->getData('from')));
        $to = date("d M Y", strtotime($filterData->getData('to')));
        $period=(string)$frm." - ". (string)$to;

另一个收藏集:

$totalAccountsCollection = Mage::getModel('customer/customer')->getCollection();
        if(strlen($storeId)==1)
            $totalAccountsCollection->addFieldToFilter('website_id',array('eq'=>Mage::getModel('core/store')->load($storeId)->getWebsiteId()));
        else if($storeArr){
            $totalAccountsCollection->addFieldToFilter('website_id',array('eq'=>Mage::getModel('core/store')->load($storeArr[0])->getWebsiteId()));
        }
        $totalAccounts=count($totalAccountsCollection);

再次加载了其他收藏集:

$CustomersCollection1 = Mage::getModel('customer/customer')->getCollection()
            ->addAttributeToFilter('created_at',array('from'=>$fromDate,'to'=>$toDate));;
        $i=0;
        foreach ($CustomersCollection1->getData() as $customer) {
            $salesOrderCollection2 = Mage::getModel('sales/order')->getCollection()
                ->addAttributeToFilter('customer_id', array('eq' => $customer['entity_id']));
            if(!count($salesOrderCollection2->getData())) {
                $i++;
            }
        }
        $customer_having_account_but_no_order = $i;

重新加载其他一些信息:

/* Total Customer Of USD Website starts */

        $USDCustomersCollection = Mage::getModel('customer/customer')->getCollection();
        $USDCustomersCollection
            ->addFieldToFilter('website_id',array('eq'=>1))
            ->addAttributeToFilter('created_at',array('from'=>$fromDate,'to'=>$toDate));;
        $USDAccounts=count($USDCustomersCollection->getData());

        /* Total Customer Of USD Website ends */



        /* Total Customer Of EURO Website starts */

        $EUROCustomersCollection = Mage::getModel('customer/customer')->getCollection();
        $EUROCustomersCollection
            ->addFieldToFilter('website_id',array('eq'=>2))
            ->addAttributeToFilter('created_at',array('from'=>$fromDate,'to'=>$toDate));;
        $EUROAccounts=count($EUROCustomersCollection->getData());

        /* Total Customer Of EURO Website starts */

最后从中选择列:

$collection->getSelect()
            ->reset(Zend_Db_Select::COLUMNS)
            ->columns('COUNT(*) as total_orders')
            ->columns('SUM(custom_attributes.is_first_order) as first_order')
            ->columns('SUM(main_table.customer_is_guest) as is_guest')
            ->columns('COUNT(*) - (SUM(main_table.customer_is_guest) + SUM(custom_attributes.is_first_order)) as returning_accounts');

        $collection->getSelect()->columns(array('new_accounts_created' => new Zend_Db_Expr($newCustomers)))
                                 ->columns(array('period' => new Zend_Db_Expr("'".$period."'")))
                                 ->columns(array('customer_having_account_but_no_order' => new Zend_Db_Expr($customer_having_account_but_no_order)))
                                 ->columns(array('usd_customer_account' => new Zend_Db_Expr($USDAccounts)))
                                 ->columns(array('euro_customer_account' => new Zend_Db_Expr($EUROAccounts)))
                                ->columns(array('total_accounts' => new Zend_Db_Expr($totalAccounts)));

最后将我的协作设置为prepareCollection:

$ this-> setCollection($ collection);

我必须对其进行优化,并在最终设置为$ this-> setCollection()的情况下使用此方法,结果为25个计数,但输出结果只有一行。

因此,我在网格中的分页显示25条记录,而不是1条(正在获取的行)

Check refernce image

1 个答案:

答案 0 :(得分:0)

要解决此问题,我需要编辑您的核心文件之一。由于编辑核心文件不是一个好习惯,因此我已覆盖了核心文件。 覆盖LIB模块

从magento / lib / Varien / Data / Collection / Db.php复制的Db.php文件 将其粘贴到本地目录,这样生成的文件夹结构将如下所示: magento /应用程序/代码/本地/ Varien /数据/集合/ Db.php

我尝试了很多方法 最后,我使用了getSelectCountSql()和getSize()