0

我试图在 zendframework 项目中使用 jqgrid,我试图从名为 utilizadores 的表中获取值。在萤火虫中,我收到了 ok 消息,但答案是:

<br />
<b>Warning</b>:  Creating default object from empty value in <b>C:\xampp\htdocs\Eulen2\application\controllers\UtilizadoresController.php</b> on line <b>51</b><br />
{"page":"1","total":1,"records":1,"rows":[{"id":"1","cell":["hugo silva","admin","Admin"]}]}

第 51 行是这样的:

$responce->page = $page > $total_pages ? $total_pages : $page;

这是我的代码:

utilizadores 控制器动作列表

 public function listAction()
    {
        // action body
        if($this->_request->isXmlHttpRequest())
        {
            $this->_helper->layout()->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            $page = $this->_getParam('page', 1);
            $limit = $this->_getParam('rows', 0);
            $sidx = $this->_getParam('sidx', 1);
            $sord = $this->_getParam('sord', 0);
            $totalrows = $this->_getParam('totalrows', false);
            if($totalrows)
                $limit = $totalrows;
            $employeesModel = new Application_Model_Account();
            $filters = !empty($_REQUEST['filters']) ? (array) json_decode($_REQUEST['filters']) : array();
            //$jqGridModel = new Application_Model_JqGrid();
            $employeesModelSelect = ($_GET['_search'] == 'true') ? $jqGridModel->where($employeesModel, $filters['rules']) : $employeesModel->select();
            $employeesModelSelect->order("$sidx $sord");
            $adapter = new Zend_Paginator_Adapter_DbTableSelect($employeesModelSelect);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setCurrentPageNumber($page)->setItemCountPerPage($limit);
            $count = count($employeesModel->fetchAll() );
            $total_pages = $count > 0 ? ceil($count / $limit) : 1;
            $responce->page = $page > $total_pages ? $total_pages : $page;
            $responce->total = $total_pages;
            $responce->records = $count;
            $i = 0;
            foreach($paginator as $item)
            {
                $responce->rows[$i]['id'] = $item['id_utilizador'];
                $responce->rows[$i]['cell'] = array($item['nome'], $item['username'],$item['tipo_utilizador']);
                $i++;
            }
            echo json_encode($responce);
        } else
        {
            $this->_helper->layout()->pageTitle = 'Listagem de utilizadores';
            $this->_helper->layout()->pageDescription = 'Neste seção podes listar todos os utilizadores';
        }
    }

在我的 list.phtml 中,我得到了这个:

<script>
        $(function() {
            $("#toolbar").jqGrid({ 
                caption:"Utilizadores",
                colNames:['Nome', 'Apelido','Cargo'],
                colModel:[
                    {name:'nome',index:'nome'},
                    {name:'username',index:'username'},
                     {name:'tipo_utilizador',index:'tipo_utilizaodr'}
                ],
                datatype:"json",
                height:421, 
                rownumWidth:40,
                pager:'#ptoolbar',
                rowList:[10,20,30],
                rowNum:10,
                sortname:'id_utilizador',
                sortorder:'desc',
                url:'/utilizadores/list/',
                viewrecords:true,
                width:740
            });
            $("#toolbar").jqGrid('navGrid','#ptoolbar',{del:false,add:false,edit:false,search:false});
            $("#toolbar").jqGrid('filterToolbar',{stringResult:true,searchOnEnter:false});
        });
    </script>

和我的账户模型

class Application_Model_Account extends Zend_Db_Table
{
 protected $_name = 'utilizadores';
}

与数据库的连接工作正常,因为我有一个登录表单,我可以根据“utilizadores”表上存在的字段进行访问。

我试图替换代码以返回 xml 而不是 json 但同样的事情发生了......

4

1 回答 1

0

$responce 在您使用它之前没有定义,这就是您收到警告的原因。

尝试添加

$responce = new stdClass();

就在 $responce->page ...

于 2013-07-19T15:42:57.450 回答