我有以下类定义:
class DatasegmentationController
{
public function indexAction()
{
$options['permissions'] = array(
'canDelete' => false,
'canEdit' => false
);
if ($this->getRequest()->isXmlHttpRequest()) {
$table = $this->getRequest()->getParam('table');
if ($table !== '' && $table !== null) {
$utilStr = new UtilString();
// This is a patch because class and tables names does not match
// so far it only happens with company and this is only for
// instantiate the proper class dynamically
$param_table = $table;
$table = $table === 'companies' ? 'company' : $table;
$classObj = strpos($table, '_') !== false ? $utilStr->stringToCamelCase($table, '_') : $utilStr->stringToCamelCase($table);
$className = new $classObj();
$module_map = $field_map[$param_table];
/** @var $module_map array */
$fields = [];
foreach ($module_map as $key => $value) {
$fields[] = [
'id' => $key,
'text' => $key
];
}
$conditions = json_decode($this->_request->getParam('conditions'), true);
$dynDataGridName = "DataSegmentation{$this->classObj}Grid";
$dynMethodName = "get{$this->classObj}GridModel";
$gridObj = new $dynDataGridName(
$this->className->$dynMethodName($conditions),
$this->view->users_id,
"{$table}_list",
"{$table}.{$table}_id",
'/datasegmentation/index',
'editor',
$options
);
return $this->_helper->json([
'fields' => $fields,
'grid' => $gridObj->getGridJs()
]);
}
if (isset($classObj, $className, $gridObj)) {
$page = $this->_request->getParam('page', 1);
$limit = $this->_request->getParam('rows', 20);
$col = $this->_request->getParam('sidx', 1);
$order = $this->_request->getParam('sord', 0);
$search = $this->_request->getParam('val', null);
echo $gridObj->getData($page, $limit, $col, $order, $search);
}
}
}
}
上面的代码的作用如下:
- 该 URL
http://localhost/datasegmentation
被称为 - 视图呈现一个选择元素(
modules
) - 更改后,我将
select#modules
其值作为 URL 的一部分发送,因此下一个 AJAX 调用变为:(http://localhost/datasegmentation?table=companies
例如) - 然后该
indexAction()
函数执行条件为何时$table
不为空或不为空 - 在所有这些东西中,它尝试动态生成所有内容,正如您在代码中可能注意到的那样。
- 其中之一是
$gridObj
动态indexAction()
网格( - 在视图中呈现网格后,它会进行 AJAX 调用并再次
indexAction()
调用它,它会跳过表的条件,因为未设置参数并尝试第二个条件,但令人惊讶的是它失败了,因为代码需要工作的对象是走了。
遇到这种情况,我的问题是:
问题
- 第二个 AJAX 调用是将数据添加到网格中,它依赖于动态参数。这是我需要解决的问题才能使其正常工作。
我不知道这是否有用,但这是在 Zend Framework 1 项目之上使用 PHP 5.5.x 进行测试和开发的。