2

大家好,我在 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();
        }
}

最后在我的视图文件中,我有一个带有 default.php 的 tmpl 文件,它显示了一个表格

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

4 回答 4

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();
于 2015-04-27T10:15:51.077 回答
0

试试这个,在控制器中将 $listaMensajes 更改为 $this->resList

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

于 2014-07-15T11:26:52.207 回答
0

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

如果这无助于查看查询在 getListaRestaurantes() 方法中返回的内容,请通过 simplle print_r($db->loadObjectList()); jexit();

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

于 2012-07-17T12:48:47.410 回答
-2

看看我们的Joomla 组件创建器。我想你会发现它很有用。

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

于 2012-07-17T12:36:09.667 回答