سؤال

أريد الحصول على العد بعد تصفية المجموعة حسب السمات. في الواقع أريد مجموعات ترتيب التصفية by non canceled ,non fraud"واستخدم getSize() وظيفة للحصول على العد ولكن يتم إعطاء ذلك count result كما هو الحال عندما any filters not applied.

يرجى اقتراح ما أفعله in this caseللحصول على النتيجة الصحيحة للعد. لدي طلب 630 وعندما أستخدمه count() وظيفة على مرشح المجموعة أنها تعطي النتيجة المناسبة.

$orderCollection = Mage::getModel('sales/order')->getCollection();

echo $orderCollection->getSize().'---'; // Result 630
                //$orderCollection->addFieldToFilter('customer_id',$customer->getId());
$orderCollection->addFieldToFilter('state', array('nin' => array('pending_payment', 'canceled', 'fraud', 'payment_review', 'pending', 'pending_paypal', 'closed')));

echo $orderCollection->size(); // result give 630


echo $orderCollection->count(); // given 305  proper result.

أعتقد أن وظيفة التحميل () قد تعمل getCount() لهذا السبب لا بأس.لقد قمت بالتحقق من الرابط الفرق بين getSize() وcount() في المجموعة

سؤالي هل مفهومي صحيح؟

هل كانت مفيدة؟

المحلول

مشكلتك انك تتصل getSize() مرتين وقمت بتعديل المجموعة بين المكالمات.
نلقي نظرة على كيف getSize() يعمل.

public function getSize()
{
    if (is_null($this->_totalRecords)) {
        $sql = $this->getSelectCountSql();
        $this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
    }
    return intval($this->_totalRecords);
} 

لذلك في المرة الأولى التي تتصل فيها getSize() تحصل على النتيجة الصحيحة 630.
ثم تقوم بإضافة مرشح آخر إلى المجموعة والاتصال getSize() مرة أخرى.
لكن في المرة الأولى التي تتصل فيها getSize() يتم تذكر القيمة في $this->_totalRecords.
في المرة الثانية التي تتصل بها، لا يتم تنفيذ الاستعلام مرة أخرى بسبب $this->_totalRecords ليست فارغة وتقوم الطريقة فقط بإرجاع القيمة التي قمت بتعيينها بالفعل، 630 في حالتك.

يمكنك إزالة الأول getSize()اتصل إذا لم تكن في حاجة إليها.
إذا كنت في حاجة إليها يمكنك القيام بذلك:

//clone the collection object
$clone = clone $orderCollection;
//get the total size from the clone
echo $clone->getSize();
//add the filter to the original object
$orderCollection->addFieldToFilter('state', array('nin' => array('pending_payment', 'canceled', 'fraud', 'payment_review', 'pending', 'pending_paypal', 'closed')));
//get the size of the filtered collection
echo $orderCollection->size(); 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top