AndrewBramwell
AndrewBramwell

Reputation: 494

Custom Magento Module

So im creating a module in the backend, I have a shell module created (items in admin top menu and a page to visit.) basically I want to have an input field that the admin can type a number into then click a button "add", this will insert a row into an existing table in the database.

$connection = Mage::getSingleton('core/resource')->getConnection('core_write');

$connection->beginTransaction();
$fields = array();
$fields['name']= 'andy';
$connection->insert('test', $fields);
$connection->commit();

I have a table called "test" within my database. If I put the above code into my Controller file, it successfully adds a row to the database when i visit the admin page. But i need to allow the user to input the data that is inserted.

Would I have to move that code into the Model and somehow send the input data to the Model and let that do the work? or not. If this is correct could someone point me to a good place to research sending data to models? (if thats even possible)

iv tried lots of tutorials but they are all way to big for what I need, I dont need to display anything, I just need to have an input box and a save button.

EDIT

i have created a file block/Adminhtml/Form/Edit/Form.php which contains the following . . .

class AndyBram_Presbo_Block_Adminhtml_Form_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form(
array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/test'),
'method' => 'post',
)
);

$form->setUseContainer(true);
$this->setForm($form);

$fieldset = $form->addFieldset('display', array(
'legend' => 'Display Settings',
'class' => 'fieldset-wide'
));

$fieldset->addField('label', 'text', array(
'name' => 'label',
'label' => 'Label',
));

if (Mage::registry('andybram_presbo')) {
$form->setValues(Mage::registry('andybram_presbo')->getData());
}

return parent::_prepareForm();
}
}

then in my controller i have 2 functions like below . . .

public function indexAction()
    {
        $this->loadLayout();

        $this->_addContent($this->getLayout()->createBlock('presbo/adminhtml_form_edit_form'));



    }   
    public function testAction()
    {
        echo 'form data here';
        $this->loadLayout();
        $this->renderLayout();

    }

the form is displayed successfully but there is no button to send or say 'do an action'

Further Edit

i have successfully added a submit button to the form that successfully goes to the testAction and echo' "form data here".

how do i then access the data,

iv added the below line

$postData = $this->getRequest()->getPost();

now if i echo $postData, it just puts "array"

if i echo $postData[0] it doesnt put anything just a blank page

any ideas or pointers?

Upvotes: 1

Views: 1104

Answers (1)

Andrey Tserkus
Andrey Tserkus

Reputation: 3634

Magento is built as an MVC framework, thus you're right - you need to pass data from controller to the model, and do not do any DB updates directly in a controller's code. The best source for an example is the own Magento code - you can take any controller action, which saves data to DB to see, how it is done. E.g. check app/code/core/Mage/Adminhtml/controllers/NotificationController.php method markAsReadAction().

There you can see, that:

  • Data is retrieved from the request by calling $this->getRequest()->getParam('id') - actually this is the answer to the question, how to get the submitted data
  • Data is set to model, and then saved to the DB via call to the
    $model->setIsRead(1)->save()

It is strongly encouraged to follow the same approach of working with models. This makes codes much better and easier to support.

Note, that "M" letter of "MVC" architecture in Magento is represented by two layers: Models and Resource Models.

Models:

  • Contain business logic of an entity. E.g. adding ten items to a Shopping Cart model triggers a discount rule
  • Represented by classes with a general name of <Your_Module>_Model_<Model_Name>
  • If need to work with DB, then extend Mage_Core_Model_Abstract and have a Resource Model, which is responsible for DB communication
  • Do not need to have basic save/load methods to be implemented, as the ancestor Mage_Core_Model_Abstract already has all that routines ready to use
  • Created via call to Mage::getModel('<your_module>/<model_name>')

Resource Models:

  • Serve as DB abstraction layer, thus save/load data from DB, perform other DB queries
  • Extend Mage_Core_Model_Resource_Db_Abstract in order to communicate with DB
  • Represented by classes with a general name of <Your_Module>_Model_Resource_<Model_Name>
  • Automatically created by a corresponding model, when it needs to communicate with DB

So, in a controller you care about creating Models only. Resource Models are created by a Model automatically.

And, according to everything said above, your controller should look like:

public function testAction()
{
    $model = Mage::getModel('your_module/your_model');
    $model->setName('andy');
    $model->save();
}

You can download a fully working example of the thing you need here.

There can be several variations to the code provided, depending on your specific case. But it represents a general approach to implementing the thing you want.

Upvotes: 3

Related Questions