Skip to main content
I've put the code in a code block
Source Link
Sander Mangel
  • 37.5k
  • 5
  • 80
  • 148

$collection = Mage::getModel('module/model')->getCollection(); $collection->getSelect() ->join( array('options'=>$this->getTable('module/table2')), 'main_table.table1_id = options.table2_id', 'options.*' );

$collection = Mage::getModel('module/model')->getCollection();
$collection->getSelect()->join( array('options'=>$this->getTable('module/table2')),'main_table.table1_id = options.table2_id','options.*');

$collection = Mage::getModel('module/model')->getCollection(); $collection->getSelect() ->join( array('options'=>$this->getTable('module/table2')), 'main_table.table1_id = options.table2_id', 'options.*' );

$collection = Mage::getModel('module/model')->getCollection();
$collection->getSelect()->join( array('options'=>$this->getTable('module/table2')),'main_table.table1_id = options.table2_id','options.*');
added 309 characters in body
Source Link
tony09uk
  • 1.7k
  • 11
  • 41
  • 56

===EDIT===

$collection = Mage::getModel('module/model')->getCollection(); $collection->getSelect() ->join( array('options'=>$this->getTable('module/table2')), 'main_table.table1_id = options.table2_id', 'options.*' );

===EDIT===

$collection = Mage::getModel('module/model')->getCollection(); $collection->getSelect() ->join( array('options'=>$this->getTable('module/table2')), 'main_table.table1_id = options.table2_id', 'options.*' );

Source Link
tony09uk
  • 1.7k
  • 11
  • 41
  • 56

displaying data from two tables in one grid

I have a grid for my admin module and it works fine, but I now would like to add more data onto that grid, but the additional data is held in a second table in my database.

To display this additional data I have tried

protected function _prepareCollection()
{
    $collection = Mage::getModel('modulename/modelname')->getCollection();
    $collection->join(array('table2' => 'table1'), 'table1_id = table2_id', 'unsubscribed');//additional line
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    $this->addColumn('table1_id', array(
        'header'    =>  $this->__('ID'),
        'align'     => 'left',
        'width'     => '50px',
        'index'     => 'table1_id',
    ));
    
    $this->addColumn('salutation', array(
        'header'    => $this->__('Title'),
        'align'     => 'left',
        'index'     => 'salutation',
        'width'     => '80px',
    ));
    
    $this->addColumn('f_name', array(
        'header'    => $this->__('First Name'),
        'align'     => 'left',
        'index'     => 'f_name',
        'width'     => '100px',
    ));

    $this->addColumn('l_name', array(
        'header'    => $this->__('Last Name'),
        'align'     => 'left',
        'index'     => 'l_name',
        'width'     => '100px',
    ));

    $this->addColumn('subscribed', array(
        'header'    =>  $this->__('Subscribed'),
        'align'     => 'left',
        'width'     => '80px',
        'index'     => 'Subscribed',
        'type'      => 'options',
        'options'   => array(
            0 => 'Subscribed',
            1 => 'Not Subscribed',
        ),
    ));//additional column

table1 has table1_id, salutation, f_name, l_name

table2 has table2_id, subscribed

First I added the new column and that displayed fine, then I added the join statement, but that returned an

Integrity constraint violation: 1052 Column 'table1_id' in on clause is ambiguous

So its obviously something to do with $collection->join() but what have I done wrong? I wasn't sure if it was due to the order I place the tables but reordering them failed, i'm obviously missing something