Magento - संग्रह का क्रम निर्धारित करने में असमर्थ


11

यह सही ढंग से आदेश नहीं दिया जा रहा है, कुछ भी मैं गलत कर रहा हूँ? सुझाव?

$componentQuantityCollection = Mage::getModel('catalog/product')->getCollection();
$componentQuantityCollection->joinField('qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.stock_id=1',
    'left');
$componentQuantityCollection->addAttributeToFilter('sku', array('in' => $componentSkus))->setOrder('sku','ASC');

एक और संग्रह जो लगता नहीं है जो पहले की तुलना में अलग है:

$kitCollection = Mage::getModel('kitinventory/kitinventory')->getCollection()->addFieldToFilter('kit_sku', $sku)->setOrder('related_sku', 'DESC');

जवाबों:


42

EAV संग्रह विशेषताओं के साथ काम करता है, छँटाई विधि यहाँ भी थोड़ी अलग है

$componentQuantityCollection->addAttributeToSort('sku', 'ASC');

गैर-ईएवी संग्रह के लिए निम्न विधियों में से एक का उपयोग करें

$kitCollection->getSelect()->order('related_sku DESC');
$kitCollection->setOrder('related_sku', 'DESC');

दूसरे संग्रह के बारे में क्या?
— १०:२१ पर easymoden00b

यह एक फ्लैट प्रकार का संग्रह है, इसलिए कोई ईएवी और विशेषताएँ नहीं है। : कैसे कि सॉर्ट करने के लिए पर इस जवाब पर एक नजर डालें stackoverflow.com/a/11354060
— Sander Mangel

मैंने सेटऑर्डर ('संबंधित_स्कू', 'डीईएससी') की कोशिश की; लेकिन इसे हल नहीं किया गया है।
— १०:२६ पर easymoden00b

मैं अपने जवाब संपादित किया है
— Sander Mangel

2
@ easymoden00b इस का उपयोग$kitCollection->getSelect()->order('related_sku DESC');
— प्रियंक

15

आप इस तरह के क्रम जोड़ सकते हैं:

$kitCollection->getSelect()->order('related_sku DESC');

अधिक जानकारी: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento

आशा है कि आप मदद कर सकते हैं।


3
यह मेरे लिए तय है, क्योंकि मैं इसे इस्तेमाल कर रहा था->order('related_sku', 'desc');
— रिकी ओडिन मैथ्यू

3

यहां अन्य उत्तरों पर विस्तार करने के लिए, $kitCollection->getSelect()->order('column DESC')ठीक काम करता है, लेकिन आप एक से अधिक कॉलम नहीं जोड़ सकते। उदाहरण के लिए, $kitCollection->getSelect()->order('column DESC, column2 ASC')त्रुटि होगी। यह उस कार्य के कारण है जो Magento स्तंभ नामों से बचने के लिए करता है। इसके आस-पास जाने के लिए, आप इसका उपयोग कर सकते हैं Zend_Db_Expr:

$kitCollection->getSelect()->order(new Zend_Db_Expr('related_sku DESC, column2 ASC'));

1

easymoden00b, setOrder()उत्पाद पर ईएवी संरचना के कारण काम नहीं कर रहा है। @ शेन्डे addAttributeToSort()फ़ंक्शन का उपयोग करने के लिए कहते हैं , क्योंकि

  • Magento is join multiple tables for product collection.

  • Attribute alias name at collection

  • setOrder() functionजब यह order expression Fieldname, SortOrder है तब काम कर रहा है correct।

आप देख सकते हैं, कि मैगनेटो किस तरह से फ़ील्ड उर्फ ​​बनाता है और यह वर्ग तालिका गुण से संबंधित है Mage_Eav_Model_Entity_Collection_Abstract

public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
{
    if (isset($this->_joinFields[$attribute])) {
        $this->getSelect()->order($this->_getAttributeFieldName($attribute).' '.$dir);
        return $this;
    }
    if (isset($this->_staticFields[$attribute])) {
        $this->getSelect()->order("e.{$attribute} {$dir}");
        return $this;
    }
    if (isset($this->_joinAttributes[$attribute])) {
        $attrInstance = $this->_joinAttributes[$attribute]['attribute'];
        $entityField = $this->_getAttributeTableAlias($attribute) . '.' . $attrInstance->getAttributeCode();
    } else {
        $attrInstance = $this->getEntity()->getAttribute($attribute);
        $entityField = 'e.' . $attribute;
    }

    if ($attrInstance) {
        if ($attrInstance->getBackend()->isStatic()) {
            $orderExpr = $entityField;
        } else {
            $this->_addAttributeJoin($attribute, 'left');
            if (isset($this->_joinAttributes[$attribute])||isset($this->_joinFields[$attribute])) {
                $orderExpr = $attribute;
            } else {
                $orderExpr = $this->_getAttributeTableAlias($attribute).'.value';
            }
        }

        if (in_array($attrInstance->getFrontendClass(), $this->_castToIntMap)) {
            $orderExpr = Mage::getResourceHelper('eav')->getCastToIntExpression(
                $this->_prepareOrderExpression($orderExpr)
            );
        }

        $orderExpr .= ' ' . $dir;
        $this->getSelect()->order($orderExpr);
    }
    return $this;
}

1

यहां एक विन्यास योग्य उत्पाद की विशेषताओं में विकल्पों के क्रम को क्रमबद्ध करने के लिए मेरा समाधान है। Collection.php की प्रतिलिपि बनाकर प्रारंभ करें,

app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.phpको app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php।

तब आप इस कोड को पा सकते हैं:

foreach ($this->getProduct()->getTypeInstance(true)->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct()) as $associatedProduct) {

और इसे इस कोड से बदलें:

$assProds = $this->getProduct()->getTypeInstance(true)->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct());
sort($assProds);
foreach ($assProds as $associatedProduct) {

यह आपको विशेषता विकल्पों की ड्रॉप-डाउन सूची को वर्णानुक्रम में क्रमबद्ध करने की अनुमति देगा। आप array_reverse()इसी तरह के कार्यों का उपयोग करके आदेश को उल्टा भी कर सकते हैं ।

पहले, मेरे विशेषता विकल्प रिवर्स वर्णमाला क्रम में थे। अब, वे वर्णानुक्रम में हैं।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.