dtcd27183 2015-01-22 09:43
浏览 45

自定义Joomla 3按钮任务未在我的控制器中执行

I am trying to upload an external list of "groups" to add to my custom Joomla 3 component. I have created a CSV file and written a few functions that I hope will do it. I have created a custom button to start the task in my "groups" view.

When I push the button I get an SQL error that has absoloutle nothing to do with the functions so I have tried debugging and when the button is pressed its not even getting to my controller task before the sql error. I am so confused as to why.

This is the code I have

view.html.php TestViewGroups

JToolBarHelper::custom('group.uploadsave', '', '', 'Upload and Save', false);

TestControllerGroup

protected function uploadsave() {



    $detail_headers = array(                
            'agm_date',
            'preferred_media'
    );



    $rows = array_map('str_getcsv', file('groupdata.csv'));
    $header = array_shift($rows);

    foreach ($rows as $row) {
        $entry = array_combine($header, $row);
        foreach ($entry as $key => $value) {
            if(in_array($key, $detail_headers)){
                $details[$key]= $value;
                unset($entry[$key]);
            }
        }
        $entry['details'] = $details;

        $this->saveUploaded($entry);
    }

    // Redirect to the list screen.
    $this->setRedirect(
            JRoute::_(
                    'index.php?option=' . $this->option . '&view=' . $this->view_list
                    . $this->getRedirectToListAppend(), false
            )
    );

}

protected function saveUploaded($dataIn = array()) {

    $app   = JFactory::getApplication();
    $lang  = JFactory::getLanguage();
    $model = $this->getModel();
    $table = $model->getTable();
    $data  = $dataIn;

    $checkin = property_exists($table, 'checked_out');
    // Determine the name of the primary key for the data.
    if (empty($key))
    {
        $key = $table->getKeyName();
    }

    // To avoid data collisions the urlVar may be different from the primary key.
    if (empty($urlVar))
    {
        $urlVar = $key;
    }

    $recordId = $this->input->getInt($urlVar);

    // Populate the row id from the session.
    $data[$key] = $recordId;

    if (!$model->save($validData))
    {

        // Redirect back to the edit screen.
        $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
        $this->setMessage($this->getError(), 'error');  

    }
    if ($checkin && $model->checkin($validData[$key]) === false)
    {
        // Save the data in the session.
        $app->setUserState($context . '.data', $validData);

        // Check-in failed, so go back to the record and display a notice.
        $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()));
        $this->setMessage($this->getError(), 'error');      

    }
    $this->setMessage(
            JText::_(
                    ($lang->hasKey($this->text_prefix . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS')
                            ? $this->text_prefix
                            : 'JLIB_APPLICATION') . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS'
            )
    );
}

I am not using this as a regular function, its just a once off to upload the data initially.

The SQL error I am getting is like it is trying to load a list of groups?? not anything to do with the save function at all.

The saveUploaded is a similar function to the initial save function.

Thanks :-)

**** Edit *****

I have just followed the task through with debug and its getting to the execute task methotd of JControllerLegacy and because the task is not defined in the task map its defaulting to display, hence the SQL error trying to load a group when it doesn't have an ID. Do I need to now register a task in the task map before it will pick it up?

  • 写回答

1条回答 默认 最新

  • duanji4449 2015-01-23 09:00
    关注

    I am officially an idiot! When I just logged back on to see if anyone had responded I saw that I had declared the function as a protected function!! dir! I just copied and pasted from another function and forgot to change its access. I also made a few other changes and now it works quite well!

    public function uploadsave() {      
        // An array of headers that will need to be entered into a seperate array to allow entry as JSON
        $detail_headers = array(                
                'agm_date',
                'preferred_media'
        );
    
        $app   = JFactory::getApplication();
        $lang  = JFactory::getLanguage();
        $model = $this->getModel();     
    
        $path = JPATH_COMPONENT . '/controllers/groupdata.csv';     
    
        //Load the file and pass each line into an array.
        $rows = array_map('str_getcsv', file($path));
        //Take out the first line as it is the headers.
        $header = array_shift($rows);
    
        //turn each of the arrays into an entry
        foreach ($rows as $row) {
            $entry = array_combine($header, $row);
            foreach ($entry as $key => $value) {
                //separate each of the entries that need to be entered into an array to be stored as JSON
                if(in_array($key, $detail_headers)){
                    $details[$key]= $value;
                    unset($entry[$key]);
                }
            }
            $entry['details'] = $details;
    
            $recordId = 'id';
    
            // Populate the row id from the session.
            $entry[$key] = $recordId;
    
            //Save each one
            if (!$model->save($entry))
            {
    
                // Redirect back to the edit screen.
                $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
                $this->setMessage($this->getError(), 'error');
    
                return false;
    
            }           
            $this->setMessage(
                    JText::_(
                            ($lang->hasKey($this->text_prefix . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS')
                                    ? $this->text_prefix
                                    : 'JLIB_APPLICATION') . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS'
                    )
            );
            }
    
        // Redirect to the list screen.
        $this->setRedirect(
                JRoute::_(
                        'index.php?option=' . $this->option . '&view=' . $this->view_list
                        . $this->getRedirectToListAppend(), false
                )
        );
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?