2
votes

Hi everyone I did a component for back-end in joomla 2.5, but I have problem to execute the sql query, my variable is empty so it don´t show me nothing.

I have other file and documents, but here the important for my question.

first in my controller.php I have this inside admin file

class BusquedaController extends JController
{
    protected $default_view= 'restaurantes';
    public function display(){
     parent::display();
    }
}

in my Model file I have restaurante.php

class BusquedaModelRestaurante extends JModelList{
    function getListaRestaurantes(){
        $db= JFactory::getDBO();
        $sql= "SELECT * FROM #__restaurantes";
        $db->setQuery($sql);
        return $db->loadObjectList();   
    }
}

in my controller File I have this

class BusquedaControllerRestaurantes extends JControllerAdmin
{

    public function getModel($name = 'Restaurante', $prefix = 'BusquedaModel', $config = array('ignore_request' => true))
    {
        $model = parent::getModel($name, $prefix, $config);
        return $model;
    }

    function listado(){
        $firephp->log('hola');
        $view=& $this->getView('restaurantes', 'html');
        $model= $this->getModel("restaurante");
        $listaMensajes= $model->getListaRestaurantes();
        $view->assignRef('resList', $listaMensajes);
        $view->display();
        }
}

finally in my View File I have a tmpl file with my default.php that show a table

foreach ($this->resList as $item):
        $checked=JHTML::_('grid.id', $n, $item->id); ?>
            <tr>
                <td><?php echo $checked; ?></td>
                <td><?php echo $item->id; ?></td>
                <td><?php echo $item->nombre; ?></td>
                <td><?php echo $item->direccion; ?></td>
                <td><?php echo $item->telefono; ?></td>
                <td><?php echo $item->web; ?></td>
                <td><?php echo $item->tipo; ?></td>
                <td><?php echo $item->zona; ?></td>
                <td><?php echo $item->metro; ?></td>
            </tr>
            <?php 

but the element reslist is empty, I don't know if I do my component well!!, someone know a tutorial or something to do a component in joomla 2.5

thanks!

4

4 Answers

0
votes

Try adding error_reporting(E_ALL) at the beginning of your component, it will hopefully show you what're you doing wrong.

If that doesn't help see what the query returns in getListaRestaurantes() method by simplle print_r($db->loadObjectList()); jexit();

P.S. In JModels you can use $this->_db to get reference to JDatabase object (instead of JFactory::getDBO())

0
votes

try this, change $listaMensajes to $this->resList in controller

$this->resList= $model->getListaRestaurantes();

0
votes

throw a run time exception

try
{
    $db->setQuery($query);
    $result = $db->loadResult(); // If it fails, it will throw a    RuntimeException 
}
catch (RuntimeException $e)
{
    throw new Exception($e->getMessage());
}

also in controller declare a variable as protected

protected $resList;

assign the values to variable like

$this->resList = $model->getListaRestaurantes();
-2
votes

Take a look at our Joomla Component Creator. I think you will find it useful.

I would recommend sticking to the MVC framework as much as possible and use getItems() etc. Just copy what com_weblinks is doing. Or better yet - let the component creator do it all for you.