A joined field with this alias is already declared

Hi everyone,

I have to show the product collection but with different cat filter on same page, but when I’m trying to call the same block function 2nd time on same .phtml page then it show error, so can any one help me to know how to call same block function twice on same page.

What does exactly error it show?

it shows

We’re sorry, an error has occurred while generating this email.

This is function

public function getLatestCollection($cat)
{

    $limit = $this->getProductLimit();
    $this->_collection->clear()->getSelect()->reset('where');
    $collection = $this->_collection
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents()
    ->addStoreFilter()
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('image')
    ->addAttributeToFilter('price', array('gt' => 0))
    ->addAttributeToFilter('is_saleable', 1, 'left')
    ->addAttributeToSort('created_at', 'desc');

    $collection->joinField(
        'position',
        'catalog_category_product',
        'position',
        'product_id=entity_id',
        'category_id='.$cat,
        'right'
    );

    $collection->getSelect()
    ->order('rand()')
    ->limit($limit);

    $this->_productCollection = $collection;
    return $this->_productCollection;
}

i have to use it twice,

so i’m calling it in .phtml file like this

$_proCollection = $block->getLatestProductCollection(8);

$_productCollection = $block->getLatestProductCollection(11);

But when i’m using it 2nd time its show error.

but when i remove

$collection->joinField(
‘position’,
‘catalog_category_product’,
‘position’,
‘product_id=entity_id’,
‘category_id=’.$cat,
‘right’
);

this part of code from block it works but it is neccessary to filter products on basis of category.

How to diagnose the «We’re sorry, an error has occurred while generating this email» failure.

Thanks Dimitry,

[2016-01-28 09:45:21] main.CRITICAL: exception ‘Magento\Framework\Exception\LocalizedException’ with message ‘A joined field with this alias is already declared.’ in /opt/lampp/htdocs/gauntletGallery/vendor/magento/module-eav/Model/Entity/Collection/AbstractCollection.php:740
Stack trace:
#0 /opt/lampp/htdocs/gauntletGallery/app/code/Solwin/FeaturedPro/Block/Home/FeaturedList.php(166): Magento\Eav\Model\Entity\Collection\AbstractCollection->joinField(‘position’, ‘catalog_categor…’, ‘position’, ‘product_id=enti…’, ‘category_id=8’, ‘right’)

This is the error i got.

When I remove this category filter

$collection->joinField(
‘position’,
‘catalog_category_product’,
‘position’,
‘product_id=entity_id’,
‘category_id=’.$cat,
‘right’
);

form products collection, it works but it is necessary.