4

我有以下类定义:

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);
            }
        }
    }
}

上面的代码的作用如下:

  • 该 URLhttp://localhost/datasegmentation被称为
  • 视图呈现一个选择元素(modules )
  • 更改后,我将select#modules其值作为 URL 的一部分发送,因此下一个 AJAX 调用变为:(http://localhost/datasegmentation?table=companies例如)
  • 然后该indexAction()函数执行条件为何时$table不为空或不为空
  • 在所有这些东西中,它尝试动态生成所有内容,正如您在代码中可能注意到的那样。
  • 其中之一$gridObj动态indexAction()网格(
  • 在视图中呈现网格后,它会进行 AJAX 调用并再次indexAction()调用它,它会跳过表的条件,因为未设置参数并尝试第二个条件,但令人惊讶的是它失败了,因为代码需要工作的对象是走了。

遇到这种情况,我的问题是:

  • 如何在 AJAX 调用之间保持对象活动?存储在会话变量中?还有其他解决方法吗?
  • 如果答案是将它们存储在会话变量中,是否值得推荐?关于这个这个这个等的答案呢?
  • 你会怎么处理这个?

问题

  • 第二个 AJAX 调用是将数据添加到网格中,它依赖于动态参数。这是我需要解决的问题才能使其正常工作。

我不知道这是否有用,但这是在 Zend Framework 1 项目之上使用 PHP 5.5.x 进行测试和开发的。

4

1 回答 1

2

如何在 AJAX 调用之间保持对象活动?存储在会话变量中?还有其他解决方法吗?

将数据存储在会话变量中 将数据存储在文件中,带有客户端 ID(可以是登录名、随机数、IP 等) 将数据存储在数据库中。

如果答案是将它们存储在会话变量中,是否值得推荐?关于这个、这个和这个等等的答案呢?

如果要存储关键数据,请使用端到端加密、SSL、HTTPS。

你会怎么处理这个?

使用会话变量。

于 2016-10-31T23:09:15.780 回答