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(たとえば)
  • 次に、関数は、が空でないか、null でないindexAction()場合の条件にあるものを実行します$table
  • コードでわかるように、これらすべてのものの中で、すべてを動的に生成しようとします。
  • これらのものの 1 つは動的グリッド ( $gridObj) で、同じものへの AJAX 呼び出しがありますindexAction()が、レンダリング後にデータを入力するためのパラメーターはありません。
  • グリッドがビューでレンダリングされた後、AJAX 呼び出しが行われ、再びindexAction()呼び出され、パラメーターが設定されていないためテーブルの条件をスキップし、2 番目の条件を試行しますが、コードが機能する必要があるオブジェクトがなくなった。

そのシナリオを持つ私の質問は次のとおりです。

  • AJAX 呼び出し間でオブジェクトを存続させるにはどうすればよいですか? セッション変数に保存しますか? 他の回避策はありますか?
  • 答えがセッション変数に保存されている場合、それは推奨されますか? thisthisthisの答えはどうですか?
  • これをどのように処理しますか?

問題

  • 2 番目の AJAX 呼び出しは、グリッドにデータを追加するものであり、動的パラメーターに依存しています。これは、これを機能させるために解決する必要があるものです。

これがまったく役立つかどうかはわかりませんが、これは PHP 5.5.x を使用して Zend Framework 1 プロジェクトの上でテストおよび開発されています。

4

1 に答える 1