0

I'm trying to join product and my custom table but it's returning error

Unrecognized method 'isLoaded()'

my code is:

 /**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Backend\Helper\Data $backendHelper
 * @param \Learning\Helloworld\Model\ResourceModel\Subscriber\CollectionFactory $collectionFactory
 * @param \Learning\Helloworld\Model\Status $status
 * @param \Magento\Framework\App\ResourceConnection $resource
 * @param \Psr\Log\LoggerInterface $loggerInterface
 * @param ProductFactory $productFactory
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Helper\Data $backendHelper,
    \Learning\Helloworld\Model\ResourceModel\Subscriber\CollectionFactory $collectionFactory,
    \Learning\Helloworld\Model\Status $status,
    \Magento\Framework\App\ResourceConnection $resource,
    \Psr\Log\LoggerInterface $loggerInterface,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    array $data = []
) {
    $this->_status = $status;
    $this->_collectionFactory = $collectionFactory;
    $this->_productFactory = $productFactory;
    $this->_resource = $resource;
    $this->_log = $loggerInterface;
    parent::__construct($context, $backendHelper, $data);
}

/**
     * Prepare grid collection object
     *
     * @return $this
     */
    protected function _prepareCollection()
    {
        $collection = $this->_collectionFactory->create();         

        $productCollection =  $this->_productFactory->create()->getCollection();
        $select = $productCollection->getSelect();
        $tempCollection = $collection->getSelect();

        $temp =  $tempCollection->join(
            array('product' => $this->_resource->getTableName('catalog_product_entity')),
            'main_table.product_id = product.entity_id'
         );

        $this->_log->debug((string)$temp);

        $this->setCollection($temp);

        return parent::_prepareCollection();
    }

When I debug the sql query it's returning

SELECT `main_table`.*, `product`.* FROM `custom_info` AS `main_table`
 INNER JOIN `catalog_product_entity` AS `product` ON main_table.product_id = product.entity_id

and it's returning result also but still i'm getting isLoaded() Error?

Could you please let me know if my query wrong?

1
  • @SohelRana, Any suggestions?
    – Bojjaiah
    Jan 13, 2016 at 6:14

1 Answer 1

1

Remove '$temp = ' and $this->setCollection($tempCollection);, So code look like

/**
     * Prepare grid collection object
     *
     * @return $this
     */
    protected function _prepareCollection()
    {
        $collection = $this->_collectionFactory->create();         

        $productCollection =  $this->_productFactory->create()->getCollection();
        $select = $productCollection->getSelect();
        $tempCollection = $collection->getSelect();

        $tempCollection->join(
            array('product' => $this->_resource->getTableName('catalog_product_entity')),
            'main_table.product_id = product.entity_id'
         );

        $this->setCollection(tempCollection);

        return parent::_prepareCollection();
    }
1
  • thanks for posting answer. your code also not working. see i have update your answer it's working fine.
    – Bojjaiah
    Jan 13, 2016 at 9:48

Your Answer

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

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