2

I am new to the StackExchange and this is my first question, so take it easy with me, please. :)

I am following to the letter this tutorial here, learning how to list, create, update and delete resources in the database, using Adminhtml grid's and forms.

I was able to do all the tasks proposed by the tutorial and got it up and running.

But now I want to add a dropdown field to the form, in order to grab items from another table, linked by foreign key.

The question below is most likely what I need, except the fact it is grabbing the select data from an array:

How to add dropdown field for select custom option from admin?

Does anyone have an example on how to do the above, but grabbing the select data from a database table?

1

Remembering we are following this tutorial: http://markshust.com/2012/07/05/creating-magento-adminhtml-grids-simplified

The short answer is...

Create the function below to serve as source to dropdown in the page app\code\community\Foo\Bar\Block\Adminhtml\Baz\Edit\Form.php

protected function _getChuz()
{
  $collection = Mage::getModel('foo_bar/chuz')->getCollection();
  $data_array=array();
  foreach($collection as $item) {
    $data_array[]=array('value'=>$item['id'],'label'=>$item['name']);
  }
  return($data_array);
}

... and add it to 'values' parameter in the 'select' field:

$fieldset->addField('chuz', 'select', array(
        'name'  => 'chuz',
        'label' => Mage::helper('checkout')->__('Chuz'),
        'title'     => Mage::helper('checkout')->__('Chuz'),
        'values'   => $this->_getChuz(),
        'required'  => true
    ));

And the long answer is...

1) Add the new entity chuz to app\code\community\Foo\Bar\etc\config.xml

<foo_bar_mysql4>
    <class>Foo_Bar_Model_Mysql4</class>
    <entities>
       ...
       <chuz>
           <table>foo_bar_chuz</table>
       </chuz>
    </entities>
</foo_bar_mysql4>

2) In the file app\code\community\Foo\Bar\sql\foo_bar_setup\mysql4-install-1.0.0.php do the following...

Add the new column chuz to the table foo_bar_baz

$table = $installer->getConnection()
...
->addColumn('chuz', Varien_Db_Ddl_Table::TYPE_CLOB, 0, array(
    'nullable'  => false,
  ), 'Chuz');
$installer->getConnection()->createTable($table);

And make the script for creating new table foo_bar_chuz

$table = $installer->getConnection()
->newTable($installer->getTable('foo_bar/chuz'))
->addColumn('id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
    'identity'  => true,
    'unsigned'  => true,
    'nullable'  => false,
    'primary'   => true,
    ), 'ID')
->addColumn('name', Varien_Db_Ddl_Table::TYPE_CLOB, 0, array(
    'nullable'  => false,
    ), 'Name');
$installer->getConnection()->createTable($table);

3) Create the Model files, as follows...

app\code\community\Foo\Bar\Model\Chuz.php

<?php
class Foo_Bar_Model_Chuz extends Mage_Core_Model_Abstract
{
  protected function _construct()
  {
    $this->_init('foo_bar/chuz');
  }
}

app\code\community\Foo\Bar\Model\Mysql4\Chuz.php

<?php
class Foo_Bar_Model_Mysql4_Chuz extends Mage_Core_Model_Mysql4_Abstract
{
  protected function _construct()
  {
    $this->_init('foo_bar/chuz', 'id');
  }
}

app\code\community\Foo\Bar\Model\Mysql4\Chuz\Collection.php

<?php
class Foo_Bar_Model_Mysql4_Chuz_Collection extends 
Mage_Core_Model_Mysql4_Collection_Abstract
{
  protected function _construct()
  {
    $this->_init('foo_bar/chuz');
  }
}

4) Finally do the steps from the Short Answer part.

5) Do not forget to insert some rows to foo_bar_chuz manually, because we do not have create form for it!

The result should be as follows:

Result image

  • If I wanted to add a reason dropdown for quantity change on a product would it be the same? – Vaishal Patel Dec 12 '18 at 14:32
  • Also, as of 1.6 _mysql4 is depreciated. – Vaishal Patel Dec 12 '18 at 14:43

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.