Magento using a join in grid.php prepareCollection

Can someone tell me how to make a connection in 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 connection to the faqcat table, where I use the faqcat_id key.

futher I want faqcat.name + faq.faq_id to be selected because these are the values ​​that 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 don’t know what to do next ... who wants to help me

this is the full 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();
  }

?>

to be clear, this is the sql I want to have, but then the magenta path

<?//kleurtjes
SELECT faq.faq_id as id, faqcat_name as name
FROM faq
JOIN faqcat
USING ('faqcat_id')
?>
+5
source share
2 answers

Try the following:

$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 running to get the collection:

Mage::log($collection->getSelect()->__toString());

Varien_Db_Select Zend_Db_Select, Zend.

+5

magento ( ), , . ().   , , .

$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')
                      );

/* */ /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 */

/* */

$- > printlogquery (); ;

+5

All Articles