0

im developing a custom joomla 2.5 component integrating jqGrid.

im setting up a controller task to handle (update mysql record) data sent from jqGrid postData

 var grid = jQuery(\"#list\");
            grid.jqGrid({
        onSelectRow: function(id){
            $('#list').editRow(id, true); 
        },
                url: '/index.php?option=com_nutraidev&view=products&format=raw',
                datatype: 'json',
                mtype: 'GET',
                colNames: [...],
                rowNum: 25,
                rowList: [5, 10, 20, 50],
                height: 'auto',
                pager: '#pager',
                loadonce: true,
                sortname: 'ID',
                viewrecords: true,
                direction:'RTL',
                autowidth: true,
                sortorder: \"desc\",
                caption: 'abc',
                width: '70%',
        editurl:'/index.php?option=com_nutraidev&view=products&task=Products.save&token=". JUtility::getToken() ."=1',   
                postData: {'code':\" \",....},
.....

this is the url which makes the request

/index.php?option=com_nutraidev&view=products&task=Products.save&token=". JUtility::getToken() ."=1&format=raw

im getting:

 error 500
    Invalid controller: name='products', format='raw'

tried it from the browser with the actual token - same result.

here is the important part of my code:

com_nutraidev - controllers - products.php

require_once JPATH_COMPONENT.'/controller.php';

    class NutraidevControllerProducts extends NutraidevController
    {

        public function &getModel($name = 'Products', $prefix = 'NutraidevModel')
        {
            $model = parent::getModel($name, $prefix, array('ignore_request' => true));
            return $model;
        }


        public function save()
        {
            // Check for request forgeries.
            JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

            // Initialise variables.
            $app    = JFactory::getApplication();
            $model = $this->getModel('Products', 'NutraidevModel');
            $data = JRequest::get('get');

            // Attempt to save the data.
            $return = $model->updItem($data);

            }

    }

com_nutraidev - models - products.php

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

class NutraidevModelProducts extends JModelList {

    public function __construct($config = array()) {
        parent::__construct($config);
    }

      public function updItem($data)
        {
        // set the variables from the passed data
        $code = $data['code'];
        $name = $data['name'];

        // set the data into a query to update the record
                $db   = $this->getDbo();
                $query  = $db->getQuery(true);
                $query->clear();
                $query->update(' #__product ');
                $query->set(' name = '.$db->Quote($name) );
                $query->where(' code = ' . (int) $code );

                $db->setQuery((string)$query);

        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
                return false;
        } else {
                return true;
                }
        }
}

com_nutraidev - views - products - view.raw.php

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

    class NutraidevViewProducts extends JView
    {

/** * Display the view */ public function display($tpl = null) { $app = JFactory::getApplication();

    $document = JFactory::getDocument();
    // Get data from the model
    $items = $this->get('Items');

    $this->state        = $this->get('State');

    $this->params       = $app->getParams('com_nutraidev');

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

    // Assign data to the view
    $response->page = 1;//JRequest::getVar('page');
    $response->total = 1;//JRequest::getVar('total');
    $response->records = count($items);
    $i=0;

     for ($i = 0; $i < count($items); ++$i) {
         $response->rows[$i]['id'] = intval($items[$i]->code); //id
         $response->rows[$i]['cell'] = array($items[$i]->code,
                                                    $items[$i]->name                                            
                                             );
    } 

    echo json_encode($response);    

        jexit();

}
        public function save($tpl = null)
        {
        echo "test";
        jexit();
        }
    }

looking at other questions that had the similar issue i double checked my

administrator/components/nutraidev/nutraidev.xml

and made sure

<files folder="site">
    <filename>controller.php</filename>

was there.

what could be the reason im getting this error ? i tried it also with view.json.php and got the same result. thanks

buzibuzi
  • 724
  • 3
  • 15
  • 27
  • How do you load your NutraidevControllerProducts ? – Pep Lainez Sep 08 '13 at 10:06
  • i added the display() in com_nutraidev - views - products - view.raw.php for you to see how this is done. – buzibuzi Sep 08 '13 at 14:46
  • You are importing the controller (I supose there's defined the NutraidevController class), but from JPATH_COMPONENT. This constant have a different value if you are using it from the Back End or you use it from Front End. If you have this class on the admin site, better change with JPATH_COMPONENT_ADMINISTRATOR – Pep Lainez Sep 08 '13 at 14:50
  • hi, i changed to require_once JPATH_COMPONENT_ADMINISTRATOR.'/controller.php'; on site/com_nutraidev/controllers/products.php - same result – buzibuzi Sep 08 '13 at 15:25
  • If you are getting an instance of the controller manually on your component entry point sometimes, don't know why, it's necessary to add the prefix. In this case, the controller prefix should be NutraidevController – Pep Lainez Sep 08 '13 at 15:40
  • i dont understand. where do you suggest to add it ? what file ? – buzibuzi Sep 08 '13 at 16:03
  • Any component has an entry point which is defined on a file on the component folder. Usually is called yourcomponent.php or yourcomponent.admin.php or something like that. When dealing whith several controllers, the right controller is loaded within this file using a call to JController::getInstance, which has the prefix as a parameter. – Pep Lainez Sep 08 '13 at 19:29
  • what you are saying is in site/com_nutraidev/nutraidev.php change "$controller = JController::getInstance('Nutraidev');" to "$controller = JController::getInstance('NutraidevController');" ? if so, it didn't work. i get this error "Invalid controller class: NutraidevControllerController" – buzibuzi Sep 09 '13 at 07:57
  • the 'problem' is that public part needs a different approach than backend. This example will help you: http://stackoverflow.com/questions/6606010/using-multiple-controllers-in-joomla-component-development – Pep Lainez Sep 09 '13 at 12:58
  • Alternatively you can rename your NutraidecControllerProducts to NutraidevController, but you will get into troubles if you need to add more controllers later. – Pep Lainez Sep 09 '13 at 13:00
  • regarding the solution from the post. isn't what im doing in com_nutraidev/nutraidev.php, the same ? keep in mind i have other controllers in "com_nutraidev/controllers" dir. they are displaying fine with index.php?option=com_nutraidev&view={controller}. problem is with view.raw.php. btw, i also created this components using "component-creator.com" – buzibuzi Sep 10 '13 at 07:35
  • The sample is using a general controller and a specific controller. The general controller is who decides which specific controller to load. Maybe a look to JPATH_COMPONENT/controller.php may bring light. – Pep Lainez Sep 10 '13 at 17:19

0 Answers0