3

我现在正在学习创建 MVC 组件。我研究了使用组件创建器创建的代码。

现在我想在编辑表单中找到点击保存按钮后的SQL插入函数,表单发送到哪里调用插入函数?

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>

如果 html 操作是 index.php/view=addstock&layout=edit

布局编辑调用到哪里?我试图找到我的整个组件,但找不到任何插入 SQL。

我也会展示我的 index.html.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;

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');
        }

    }
}
4

3 回答 3

4

您看不到保存代码,因为您的控制器和模型扩展了父类。您可以创建自己的公共函数保存在控制器和模型中或覆盖它。基本上这是它的工作原理:

  1. 调用控制器方法“Save”,验证数据并加载模型。
  2. 控制器调用模型并向他传递有效数据。
  3. Model 加载 JTable,JTable 存储数据并返回 true 或 false
  4. 模型将布尔值返回给控制器
  5. 控制器处理重定向

类位于:libraries/legacy/controller 和 libraries/legacy/model

于 2013-09-25T09:20:59.127 回答
0

简单的预存挂钩:

转到您的模型并写下:

public function save($data){

    // do stuff with $data

    return parent::save($data);

}

最好使用 prepareTable 函数:

protected function prepareTable($table)
{

        $table->fieldname = 'new_value';

}
于 2015-02-28T12:31:47.850 回答
0

尝试使用Component Creator创建您的组件,看看它是如何做到的。它可以是一个很好的学习工具。

于 2013-09-25T16:46:49.833 回答