3

I already had one custom sort by attribute which is agency_id but when i try to add new sort by attribute Location it shows me this error.

a:5:{i:0;s:48:"Joined field with this alias is already declared";i:1;s:3988:"#0 /home/test/public_html/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(695): Mage::exception('Mage_Eav', 'Joined field wi...')
#1 /home/test/public_html/app/code/local/Mage/Catalog/Block/Product/List.php(104): Mage_Eav_Model_Entity_Collection_Abstract->joinField('value', 'customer_entity...', 'value', 'entity_id=agenc...', Array, 'left')
#2 /home/test/public_html/app/code/local/Mage/Catalog/Block/Product/List.php(196): Mage_Catalog_Block_Product_List->_getProductCollection()
#3 /home/test/public_html/app/code/core/Mage/Core/Block/Abstract.php(918): Mage_Catalog_Block_Product_List->_beforeToHtml()
#4 /home/test/public_html/app/code/core/Mage/Core/Block/Abstract.php(637): Mage_Core_Block_Abstract->toHtml()
#5 /home/test/public_html/app/code/core/Mage/Core/Block/Abstract.php(581): Mage_Core_Block_Abstract->_getChildHtml('search_result_l...', true)
#6 /home/test/public_html/app/code/local/Mage/CatalogSearch/Block/Advanced/Result.php(103): Mage_Core_Block_Abstract->getChildHtml('search_result_l...')
#7 /home/test/public_html/app/design/frontend/default/default/template/catalogsearch/advanced/result.phtml(58): Mage_CatalogSearch_Block_Advanced_Result->getProductListHtml()

i did changes in
/app/code/local/Mage/Catalog/Block/Product/List.php

i added code in _getProductCollection() as below

$sortBy = $this->getRequest()->getParam('order');
$dir = $this->getRequest()->getParam('dir');
if($sortBy == "review"){
    /* added for rating */
    $this->_productCollection->joinField('review', 'review_entity_summary', 'rating_summary', 'entity_pk_value=entity_id', array('entity_type'=>1, 'store_id'=> Mage::app()->getStore()->getId()), 'left');
    /* added for rating */
}elseif($sortBy == "agency_id"){
    /* added for sort by agency id */
    $this->_productCollection->addAttributeToFilter('agency_id',array('neq'=>'1111'));
    $this->_productCollection->joinField('value', 'customer_entity_varchar', 'value', 'entity_id=agency_id', array('attribute_id'=>5), 'left');
    $this->_productCollection->setOrder('value', $dir);

}elseif($sortBy == "package_location"){
    /* added for sort by Location */
    $this->_productCollection->addAttributeToFilter('package_location',array('neq'=>'1111'));
    // $this->_productCollection->joinField('value', 'eav_attribute_option_value', 'value', 'option_id=package_location', array('attribute_id'=>197), 'left');
    // $this->_productCollection->setOrder('value', $dir);

}

before i added condition of package_location, agency_id was working fine.

i found this question but its different.

Thanks

3 Answers 3

3

It looks like the joinField call on the product collection is executed multiple times during one request. Likely, but not necessarily, this happens at the exact same line of code.

The field alias (first parameter) must be unique though. Make sure the relation is only joined once on initial collection allocation.

4
  • i did try with different alias but same error. :(
    – MageDev
    Commented Oct 23, 2015 at 8:53
  • by the way when i print collection it shows fine.
    – MageDev
    Commented Oct 23, 2015 at 8:54
  • Changing the alias does not help if it's the same line of code that causes the error.
    – mam08ixo
    Commented Oct 23, 2015 at 8:55
  • then what to do?
    – MageDev
    Commented Oct 23, 2015 at 11:24
0

It's because _getProductCollection() is called multiple times across the whole PHP process. My solution is to use a static variable inside _getProductCollection():

protected function _getProductCollection()
{
  static $joined = false;

  ......

  if (!$joined) {
    // do joinField() here
    $joined = true;
  }

  ......

}
0

Try this one by changing alias name, I have changed alias to "rate"

case 'top_rated':
                    if(!strpos((string)$collection->getSelect(), 'rate') > 0) {
                        $collection->joinField(
                            'rate',                // alias
                            'review_entity_summary',      // table
                            'rating_summary',               // field
                            'entity_pk_value=entity_id',    // bind
                            array(
                                'entity_type' => 1, 
                                'store_id' => 4
                            ),                              // conditions
                            'left'                          // join type
                        );
                        $collection->getSelect()->order('rating_summary desc'); 
                    }
4
  • i was stuck with this link can u plz help me @Umar Commented Feb 14, 2019 at 11:51
  • @divyasekar I have posted answer on ur link pls check Commented Feb 14, 2019 at 12:32
  • I will check and update u @umar thank for your effective reply Commented Feb 14, 2019 at 12:33
  • i was used this payment-gateway @umar Commented Feb 14, 2019 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.