3

I'm writing a small Joomla component for my school but there is a little problem with it.

When I go to edit a record and then save it, instead of updating that record, it saves it as a new record with updated field.

I have checked the button in view for any mistakes, but can't see any issues.

Can any one please tell me some common mistakes that could cause this issue?

Controller

class StudentControllerStudent extends JControllerForm
{
    public function __construct($config = array()) {
        parent::__construct($config);
    }
}

Model

class StudentModelStudent extends JModelAdmin
{
    public function getTable($type = 'Student', $prefix = 'StudentTable', $config = array())
    {
        return JTable::getInstance($type, $prefix, $config);
    }

        public function getForm($data = array(), $loadData = true) {
            $form = $this->loadForm('com_student.student', 'student',array('control' => 'jform', 'load_data' => $loadData));

            if (empty($form))
            {
                return false;
            }
            return $form;
        }

        protected function loadFormData() {
            return $this->getItem();
        }

}

view.html.php

class StudentViewStudent extends JViewLegacy {

    protected $state;
    protected $item;
    protected $form;

    /**
     * Display the view
     */
    public function display($tpl = null) {
        $this->state = $this->get('State');
        $this->item = $this->get('Item');
        $this->form = $this->get('Form');

        // Check for errors.
        if (count($errors = $this->get('Errors'))) {
            throw new Exception(implode("\n", $errors));
        }

        $this->addToolbar();
        parent::display($tpl);
    }


    protected function addToolbar() {
            JFactory::getApplication()->input->set('hidemainmenu', true);
            JToolbarHelper::title('edit item');
            JToolbarHelper::save('student.save');
            if (empty($this->item->id))
            {
                JToolbarHelper::cancel('student.cancel');
            }
            else
            {
                JToolbarHelper::cancel('student.cancel', 'JTOOLBAR_CLOSE');
            }
    }

}
  • 1
    Would you mind sharing your code else it's a little hard to track down the issue – Lodder Apr 4 '15 at 8:51
  • ok sir i included my source. help me – Sumer Raj Chouhan Apr 4 '15 at 10:02
  • Ok when when you're on the edit page for a student, in the URL, do you see something like &layout=edit? – Lodder Apr 4 '15 at 10:16
  • I believe the issue is not in your view file, but in model. Do you check for user id before saving data? – SD. Apr 4 '15 at 10:20
  • Check adminstartor/com_user/views/user/view.html.php and administrator/com_user/models/user.php for better reference. – SD. Apr 4 '15 at 10:25
1

Please try following:

1) You must create JTable class and add following code in it:

function __construct(&$db)
{
     parent::__construct('#__yourtablename', 'id', $db);
     // Assumes "id" should be primary key of your table.
}

2) Check in DB and make sure that you have primary key set for "#__yourtablename". Also it must be set as AUTO_INCREMENT if not then try following queries in your MySQL:

ALTER TABLE `#__yourtablename` ADD PRIMARY KEY(`id`);
ALTER TABLE `#__yourtablename` CHANGE `id` `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT;

Again above assumes that, you have id column in your table.

Note: Replace "#__" with your table prefix.

1

Try this function loadFormData() for your model:

protected function loadFormData()
{
    $data = JFactory::getApplication()->getUserState('com_student.edit.student.data', array());

    if (empty($data))
    {
        $data = $this->getItem();
    }

    return $data;
}
1

In the view of your component. components->com_mycom->view->tmpl->default.php (or edit.php, some.php) depending on your needs.

<form action="<?php echo JRoute::_( 'index.php?option=com_student&view=student&layout=ifnotdefault&id='.$this->item->id ); ?>" method="post" class="form-validate" id="adminForm" enctype="multipart/form-data">

    <?php echo $this->form->renderField('id'); ?> // !important
    <?php echo $this->form->renderField('somefield'); ?>

    <input type="hidden" name="task" value="student.save" /> // !important
        <input type="submit" value="Save" />
        <?php echo JHtml::_( 'form.token' ); ?>

</form>

Check 'form action', Check 'field id', Check 'task'

  • Please explain where this snippet should be written. How should this technique be integrated with the OP's posted code? Please be generous with your knowledge and aim educate/empower thousands of people with your answer. – mickmackusa Sep 20 at 20:17

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.