在joomla中执行sql查询

时间:2012-07-17 11:55:39

标签: joomla joomla2.5 joomla-component

大家好我在joomla 2.5中为后端做了一个组件,但是执行sql查询时遇到问题,我的变量是空的,所以它没有给我任何显示。

我有其他文件和文件,但这里对我的问题很重要。

首先在我的controller.php中我有这个内部管理文件

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

在我的模型文件中我有restaurante.php

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

在我的控制器文件中我有这个

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

最后在我的View文件中我有一个tmpl文件,我的default.php显示了一个表

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 

但是元素reslist是空的,我不知道我的组件是否运行良好!!有人知道教程或者在joomla 2.5中做某个组件的事情

谢谢!

4 个答案:

答案 0 :(得分:0)

尝试在组件的开头添加error_reporting(E_ALL),它有望向您显示您做错了什么。

如果这无法通过simplele print_r($db->loadObjectList()); jexit();

查看getListaRestaurantes()方法中的查询返回的内容

P.S。在JModels中,您可以使用$ this-&gt; _db来获取对JDatabase对象的引用(而不是JFactory :: getDBO())

答案 1 :(得分:0)

试试这个,将$ listaMensajes更改为$ this-&gt;控制器中的resList

$ this-&gt; resList = $ model-&gt; getListaRestaurantes();

答案 2 :(得分:0)

抛出运行时异常

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

也在控制器中将变量声明为受保护的

protected $resList;

将值分配给变量,如

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

答案 3 :(得分:-2)

看看我们的Joomla Component Creator。我想你会发现它很有用。

我建议尽可能多地使用MVC框架并使用getItems()等。只需复制com_weblinks正在做的事情。或者更好 - 让组件创建者为您完成所有操作。