在joomla中执行sql查询

问题描述 投票:3回答:4

大家好我在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中的组件

谢谢!

joomla joomla2.5 joomla-component
4个回答
0
投票

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

如果这没有帮助通过简单的qazxsw poi看到查询在getListaRestaurantes()方法中返回的内容

附:在JModels中,您可以使用$ this - > _ db来获取对JDatabase对象的引用(而不是JFactory :: getDBO())


0
投票

试试这个,将$ listaMensajes更改为$ this-> resList in controller

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


0
投票

抛出运行时异常


print_r($db->loadObjectList());
jexit();

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

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;

-2
投票

看看我们的$this->resList = $model->getListaRestaurantes(); 。我想你会发现它很有用。

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

© www.soinside.com 2019 - 2024. All rights reserved.