3

I'm learning to create MVC component right now. I studied the code that was created using the component creator.

Now I wanna to locate the SQL insert function after the save button is click in the edit form, where does the form send to to call the insert function?

com_astock/admin/view/addstock/tmpl/edit.php

<?php
/**
 * @version     1.0.0
 * @package     com_astock
 * @copyright   Copyright (C) 2013. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Joe <joequah1@hotmail.com> - http://
 */
// no direct access
defined('_JEXEC') or die;

JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
JHtml::_('formbehavior.chosen', 'select');
JHtml::_('behavior.keepalive');

// Import CSS
$document = JFactory::getDocument();
$document->addStyleSheet('components/com_astock/assets/css/astock.css');
?>
<script type="text/javascript">
    js = jQuery.noConflict();
    js(document).ready(function(){

    });

    Joomla.submitbutton = function(task)
    {
        if(task == 'addstock.cancel'){
            Joomla.submitform(task, document.getElementById('addstock-form'));
        }
        else{

            if (task != 'addstock.cancel' && document.formvalidator.isValid(document.id('addstock-form'))) {

                Joomla.submitform(task, document.getElementById('addstock-form'));
            }
            else {
                alert('<?php echo $this->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED')); ?>');
            }
        }
    }
</script>

<form action="<?php echo JRoute::_('index.php?option=com_astock&layout=edit&stock_code=' . (int) $this->form->getInput('stock_code')); ?>" method="post" enctype="multipart/form-data" name="adminForm" id="addstock-form" class="form-validate">
    <div class="row-fluid">
        <div class="span10 form-horizontal">
            <fieldset class="adminform">

                        <div class="control-group">
                <div class="control-label"><?php echo $this->form->getLabel('stock_code'); ?></div>
                <div class="controls"><?php echo $this->form->getInput('stock_code'); ?></div>
            </div>
                <div class="control-group">
                <div class="control-label"><?php echo $this->form->getLabel('name'); ?></div>
                <div class="controls"><?php echo $this->form->getInput('name'); ?></div>
            </div>
            <div class="control-group">
                <div class="control-label"><?php echo $this->form->getLabel('state'); ?></div>
                <div class="controls"><?php echo $this->form->getInput('state'); ?></div>
            </div>
                    <div class="control-group">
                <div class="control-label"><?php echo $this->form->getLabel('time_created'); ?></div>
                <div class="controls"><?php echo $this->form->getInput('time_created'); ?></div>
            </div>
            <div class="control-group">
                <div class="control-label"><?php echo $this->form->getLabel('created_by'); ?></div>
                <div class="controls"><?php echo $this->form->getInput('created_by'); ?></div>
            </div>


            </fieldset>
        </div>



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

    </div>
</form>

if the html the action is index.php/view=addstock&layout=edit

Where does the layout edit call to? I had try to locate my entire component I couldn't find any insert SQL.

I will be showing my index.html.php as well

<?php
/**
 * @version     1.0.0
 * @package     com_astock
 * @copyright   Copyright (C) 2013. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Joe <joequah1@hotmail.com> - http://
 */

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.view');

/**
 * View to edit
 */
class AStockViewAddstock 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);
    }

    /**
     * Add the page title and toolbar.
     */
    protected function addToolbar()
    {
        JFactory::getApplication()->input->set('hidemainmenu', true);

        $user       = JFactory::getUser();
        $isNew      = ($this->item->stock_code == 0);
        if (isset($this->item->checked_out)) {
            $checkedOut = !($this->item->checked_out == 0 || $this->item->checked_out == $user->get('stock_code'));
        } else {
            $checkedOut = false;
        }
        $canDo      = AStockHelper::getActions();

        JToolBarHelper::title(JText::_('COM_ASTOCK_TITLE_STOCK'), 'addstock.png');

        // If not checked out, can save the item.
        if (!$checkedOut && ($canDo->get('core.edit')||($canDo->get('core.create'))))
        {

            JToolBarHelper::apply('addstock.apply', 'JTOOLBAR_APPLY');
            JToolBarHelper::save('addstock.save', 'JTOOLBAR_SAVE');
        }
        if (!$checkedOut && ($canDo->get('core.create'))){
            JToolBarHelper::custom('addstock.save2new', 'save-new.png', 'save-new_f2.png', 'JTOOLBAR_SAVE_AND_NEW', false);
        }
        // If an existing item, can save to a copy.
        if (!$isNew && $canDo->get('core.create')) {
            JToolBarHelper::custom('addstock.save2copy', 'save-copy.png', 'save-copy_f2.png', 'JTOOLBAR_SAVE_AS_COPY', false);
        }
        if (empty($this->item->stock_code)) {
            JToolBarHelper::cancel('addstock.cancel', 'JTOOLBAR_CANCEL');
        }
        else {
            JToolBarHelper::cancel('addstock.cancel', 'JTOOLBAR_CLOSE');
        }

    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Ninjoe Quah
  • 399
  • 6
  • 24
  • try to follow official tutorial you can also download sample com_helloworld http://docs.joomla.org/Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5 – Jobin Sep 25 '13 at 08:14
  • i did tried to follow that, but its not working well in joomla 3 and I can't find the SQL insert function too – Ninjoe Quah Sep 25 '13 at 08:19

3 Answers3

4

You cannot see save code as your controller and model extends parent classes. You can create your own public function save in controller and model or override it. Basically this is how it works:

  1. Controller method 'Save' is called, which validates data and loads model.
  2. Controller calls Model and pass him valid data.
  3. Model loads JTable, which stores the data and returns true or false
  4. Model returns bool to controller
  5. Controller handles redirect

Classes are located in: libraries/legacy/controller and libraries/legacy/model

BenMorel
  • 34,448
  • 50
  • 182
  • 322
di3sel
  • 2,002
  • 15
  • 24
  • so if I want to override, we should I create at? the function name is called save right? – Ninjoe Quah Sep 25 '13 at 13:07
  • depends on which part of saving you want to override. Do you want to change some data before saving, or what? – di3sel Sep 25 '13 at 13:16
  • I want to control what to save into the db. I want to override the mysql insert for the above. – Ninjoe Quah Sep 25 '13 at 13:18
  • Why do you want to override it? I suggest to have a look at the existing core components like com_weblinks. As di3sel described all the stuff is handled in the controller, model and table. They take also care about the event triggering and data validation. I tried the component creators as well and they are very limited and not up to date with the most actual code of Joomla. There is also a post save hook function. If you want to control what is saved, override the chick function in your table. – Laoneo Sep 25 '13 at 13:32
  • ok I will try to look at that. Can we make the database primary key not auto increase? – Ninjoe Quah Sep 25 '13 at 13:37
  • how to prevent duplicate name? or maybe other elements in int? – Ninjoe Quah Sep 25 '13 at 15:15
  • com_weblinks is a great example of how stuff should work with MVC. It has all basic structure and overrides needed. – di3sel Sep 26 '13 at 08:05
0

Try to create your component using the Component Creator and see how it does it. It can be a great leaning tool.

Søren Beck Jensen
  • 1,676
  • 1
  • 12
  • 22
0

simple presave hook:

go to your model and write:

public function save($data){

    // do stuff with $data

    return parent::save($data);

}

better would be using the prepareTable function:

protected function prepareTable($table)
{

        $table->fieldname = 'new_value';

}
Dennis Heiden
  • 757
  • 8
  • 16