2

I have created an edit form for an admin module, but have come across an issue with putting data into and pulling data from the database.

For example I have field A,B and C, where I place some data, this is lost before it gets to the database, although a new row and default values for that row are created. If I place data directly into the database my grid which I have created displays it as well as the empty rows that have been created. I don't understand why values are not being passed?

I have this

class Training_Animal_Block_Adminhtml_Animal_Edit_Form
extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
    $form = new Varien_Data_Form(array(
        'id'        => 'edit_form',//id for the form
        'action'    => $this->getUrl('*/*/save', 
                array('id' => $this->getRequest()->getParam('id'))),//post action
        'method'    => 'post',//the method
        'enctype'   => 'multipart/form_data',//allows posting of files
    ));
    $form->setUseContainer(TRUE);
    $this->setForm($form);

    return parent::_prepareForm();
}
}

and this

class Training_Animal_Block_Adminhtml_Animal_Edit_Tab_General
extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
    //instantiates form
    $form = new Varien_Data_Form();
    $form->setHtmlIdPrefix('general');
    $fieldset = $form->addFieldset('general_form', array(
        'legend' => $this->__('General Setup')
    ));            

    if ( Mage::registry('current_animal')->getId() ){
        //creates fieldset for form
        $fieldset->addField('entity_id', 'label', array(
            'label' => $this->__('Entity id %s', 
                    Mage::registry('current_animal')->getId()
            )
        ));
    }

    $fieldset->addField('name', 'text', array(
        'label'     => $this->__('Name'),
        'class'     => 'required-entry',
        'required'  => TRUE,
        'name'      => 'name'//matches with name from entity and $form->addValues($this->getFormData()); will pass in information from entity
    ));

    $fieldset->addField('type', 'text', array(
        'label'     => $this->__('Animal Type'),
        'class'     => 'required-entry',
        'required'  => TRUE,
        'name'      => 'type'
    ));

    $fieldset->addField('edible', 'select', array(
        'label'     => $this->__('Is Edible'),
        'class'     => 'required-entry',
        'required'  => TRUE,
        'name'      => 'edible',
        'values'    => array('No', 'Yes', 'Maybe'),
        //or: Mage::getModel('training/entity_attribute_source_maybe')
        //                  ->getOptionArray(),
    ));

    //passes data from entity
    $form->addValues($this->getFormData());
    $this->setForm($form);

    return parent::_prepareForm();
}
}

I believe the above may hold the reason for the failure, but i am not sure. On checking the save action with var_dump() I can see the data is not passed.

The database columns are name, type, edible

===EDIT===

public function saveAction()
{

    if ( $data = $this->getRequest()->getPost() ) {
        $this->_getSession()->setFormData($data);
        $model = Mage::getModel('training/animal');
        $id = $this->getRequest()->getParam('id');
        try {
            if ( $id ) {
                $model->load($id);
            }
            $model->addData($data);
            //saves data
            $model->save();

            $this->_getSession()->addSuccess(
                    $this->__('Animal was saved'));

            $this->_getSession()->setFormData(false);

            if ( $this->getRequest()->getParam('back') ) {
                $params = array('id' => $model->getId());
                $this->_redirect('*/*/edit', $params);
            } else {
                $this->_redirect('*/*/list');
            }
        } catch ( Exception $e ) {
            $this->_getSession()->addError($e->getMessage());
            if ( $model && $model->getId() ) {
                $this->_redirect('*/*/edit', array(
                    'id' => $model->getId()
                ));
            } else {
                $this->_redirect('*/*/new');
            }
        }

        return;
    }
    //if no data has been passed an error will be thrown and user redirected
    $this->_getSession()->addError($this__('No data found to save'));
    $this->_redirect('*/*');
}

===EDIT===

Hope this can help with a solution

I have looked at the HTML that is being generated in the browser, and it shows

<div class="entry-edit">
    <form id="edit_form" action="http://magentodevtest.local/index.php/admin/animal/save/" method="post" enctype="multipart/form_data">
        <div>
            <input name="form_key" type="hidden" value="LmdBGhXdIcQWBm0e" />
        </div>
    </form>
</div>

name

type

edible

are all outside the form tags only form_key is within the tags and when I check in the save action only the form_key is being passed.

8
  • 1
    The field names must match the table column names.
    – Marius
    Sep 12, 2013 at 13:44
  • I thought that was the case so i checked that prior to adding this post. The second block of code I have posted has names matching the database columns. What else could I look for?
    – tony09uk
    Sep 12, 2013 at 13:48
  • Maybe you should post your controller action that saves/retrieves your entity
    – Marius
    Sep 12, 2013 at 13:49
  • I have added that now
    – tony09uk
    Sep 12, 2013 at 13:52
  • 1
    Did you clear your cache? Meta information about tables are cached so if you added new fields Magento might not recognize that. Sep 12, 2013 at 13:52

2 Answers 2

2

You are trying to find a troublesome drop in the middle of a river! I recommend to first confirm that everything is OK with your ORM using a workbench script:

<?php
header('Content-Type: text/plain);
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors',true);
include 'app/Mage.php';
umask(0);
Mage::setIsDeveloperMode();
Mage::app();

$model = Mage::getModel('training/animal');
$model->setData(
    'name' => 'Tony',
    'type' => 'tiger',
    'edible' => 'no'
);
$model->save();
print_r($model->getData()); //should have array values & auto-increment ID
0

You are missing to close the header('Content-Type: text/plain); in the first line.... it should be like header('Content-Type: text/plain');

1
  • How is that relevant at all? Answered the wrong question? Feb 20, 2015 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.