0

My Collection.php

 .... 
public function __construct(
                \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
                \Psr\Log\LoggerInterface $logger,
                \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
                \Magento\Framework\Event\ManagerInterface $eventManager,
                \Magento\Store\Model\StoreManagerInterface $storeManager,
                \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
                \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
            ) {
                $this->_init(
                    'Module\Vendor\Model\Test',
                    'Module\Vendor\Model\ResourceModel\Test'
                );
                parent::__construct(
                    $entityFactory, $logger, $fetchStrategy, $eventManager, $connection,
                    $resource
                );
                $this->logger = $logger;
                $this->storeManager = $storeManager;
                $this->store = $this->storeManager->getStore();
                $this->storeId = $this->store->getId();
                print_r($this->storeId);//THIS PRINTS ON THE PAGE CORRECT STORE ID
            }

            protected function _initSelect()
            {

                parent::_initSelect();
                $this->getSelect()->joinLeft(
                    ['secondTable' => $this->getTable('my_custom')], //2nd table name by which you want to join
                    'main_table.my_id = secondTable.my_id AND secondTable.store = '.(int)$this->storeId, // HERE DOES NOT WORK! common column which available in both table
                    '*' // '*' define that you want all column of 2nd table. if you want some particular column then you can define as ['column1','column2']
                );

            }
         ....

I printing The correct store ID Which is 1 in constructor but in _initSelect Does not seem to work/Available if hard coded secondTable.store =1' Then it works.

I don't understand Why Anyone know What could be the reason?

Appreciate!

0

Try to call the $storeId directly on function without the declaration on __contruct

    protected function _initSelect()
        {

            parent::_initSelect();
            $storeId = $this->storeManager->getStore()->getId();
            $this->getSelect()->joinLeft(
                ['secondTable' => $this->getTable('my_custom')], //2nd table name by which you want to join
                'main_table.my_id = secondTable.my_id AND secondTable.store = {$storeId}', // HERE DOES NOT WORK! common column which available in both table
                '*' // '*' define that you want all column of 2nd table. if you want some particular column then you can define as ['column1','column2']
            );

        }  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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