我有以下类定义:
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
>视图使用选项呈现选择元素(模块)
>当select#modules更改时,我将其值作为URL的一部分发送,以便下一个AJAX调用变为:http:// localhost / datasegmentation?table = companies(例如)
> indexAction()函数然后执行$table不为空或不为null时的条件
>在所有这些东西中,它试图动态生成所有内容,就像你在代码中注意到的那样.
>其中一个是dinamic网格($gridObj),它对同一个indexAction()进行了AJAX调用,但没有参数用于在渲染后填充数据
>在视图中呈现网格之后它会进行AJAX调用并再次调用indexAction()并且它跳过表的条件,因为参数未设置并尝试第二个条件但是它会因为编码的对象而失败需要工作的都没了.
有了那个场景我的问题是:
>如何在AJAX调用之间保持对象生效?存储在会话变量中?还有其他解决方法吗?
>如果答案是将它们存储在会话var中,是否值得推荐? this,this和this的答案如何?
>你会怎么处理这个?
问题
>第二个AJAX调用是向网格添加数据的调用,它依赖于动态参数.这是我需要解决的问题才能让它发挥作用.
我不知道这是否有用,但这是在使用PHP 5.5.x在Zend Framework 1项目上测试和开发的.
最佳答案
How do I keep the object alives between AJAX calls? Storing in a
session var? Any other workaround?
将数据存储在会话变量中
将数据存储在具有客户端ID的文件中(可以是登录,随机,IP等)
将数据存储在数据库中.
If the answer is store them in a session var, is it recommendable?
What about the answers on this, this and this among others?
如果要存储关键数据,请使用端到端加密,SSL,HTTPS.
How would you handle this?
使用会话变量.