magento - custom table join with catalog/product

2019-08-12 03:04发布

问题:

I am having seller_id field in my custom table sellerrequest as foreign key. Primary reference is in customer/customer_collection collection. I want to show the seller name in admin grid from the seller_id. I am not sure how to join both collections but I tried with -

$collection = Mage::getModel("wallets/sellerrequest")
                ->join(
                        'customer/customer_collection',
                        'seller_id=main_table.seller_id'
                        )
                ->getCollection();

but, it doesn't work. Is this wrong way? Any Help appreciated.

Thanks.

回答1:

Try this

$collection = Mage::getModel("wallets/sellerrequest")->getCollection();
        $collection->getSelect()->joinLeft(
            array('cust' => $collection->getTable('customer/customer_collection')),
            'cust.seller_id = main_table.seller_id');

Hope this may help.By the way I haven't tried it. But the same worked for me.See the data in the collection to check whether you get the correct data or not.

Here is another example that i have tried.

 protected function _prepareCollection(){

        $collection = Mage::getModel('children/children')->getCollection();        
        $collection->getSelect()->joinLeft('schools', 'schools.school_id = main_table.school_id', array('school_name'));

         $collection->addFieldToFilter('main_table.customer_id', array('in' => $this->_getCustomer()->getId()));

        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

Here i have joined "schools" table to my children model.In my case the common key between tables is school_id. This worked for me,check this out and make some ammendments to meet your requirement.



标签: magento