Can someone tell me how to make a join within magento
Here is the problem:
<?//kleurtjes
$collection= Mage::getModel('faq/faq')->getCollection();
$collection->getSelect()->join(array('faqcat' => $this->getTable('faqcat/faqcat')), 'faqcat.faqcat_id=faq.faqcat_id' , array('faqcat.*'));
?>
i am trying to make a join with the table faqcat where i use the key faqcat_id .
futher i want that faqcat.name + faq.faq_id are being selected cos these are the values i want to use in colums.
<?
  protected function _prepareColumns()
  {
      $this->addColumn('faq_id', array(
          'header'    => Mage::helper('faq')->__('ID'),
          'align'     =>'right',
          'width'     => '50px',
          'index'     => 'faq_id',
      ));
      $this->addColumn('name', array(
          'header'    => Mage::helper('faqcat')->__('Titel'),
          'align'     =>'left',
          'index'     => 'name',
      ));
}
?>
after trying 1000 combinations i dont know what to do anymore ... who is willing to help me
this is the complete function:
<?
  protected function _prepareCollection()
  {
     $collection= Mage::getModel('faq/faq')->getCollection();
     //$collection->getSelect()->join(array('faqcat' => $this->getTable('faqcat/faqcat')), 'faqcat.faqcat_id=faq.faqcat_id' , array('faqcat.*'));
     $id = Mage::getModel('customer/session')->getCustomer()->getId();
      $this->setCollection($collection);
     // }
      return parent::_prepareCollection();
  }
?>
just to be clear this is the sql i want to have , but then the magento way
<?//kleurtjes
SELECT faq.faq_id as id, faqcat_name as name
FROM faq
JOIN faqcat
USING ('faqcat_id')
?>
                Try this:
$collection->getSelect()
          ->join($this->getTable('faqcat/faqcat'), "faqcat.faqcat_id = main_table.faqcat_id", array(faqcat.*));
You can see the sql that will actually be run to fetch the collection by:
Mage::log($collection->getSelect()->__toString());
The Varien_Db_Select class is based on Zend_Db_Select, so the Zend documentation is a good reference.
i have just started developing magento extension (loving it) and this is the 2nd part in it where i have to show a grid from two tables. (whew). but its not that easy after surfing internet a lot i achieved mine result by doing this.
$collection = Mage::getModel('linkdirectory/linkdirectory')->getCollection();
      $resource = Mage::getSingleton('core/resource');
      $collection->getSelect()
              ->join(
                      array('lk'=>$resource->getTableName('linkdirectory/linkcategory')),
                     'lk.cat_id = main_table.cat_id',
                      array('lk.cat_title','lk.cat_id')
                      );
/* mine was showing this */
/SELECT main_table., lk.cat_title, lk.cat_id FROM linkdirectory AS main_table INNER JOIN linkcategory AS lk ON lk.cat_id = main_table.cat_id*/
/* to print the current query */
$collection->printlogquery(true);exit;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With