Search code examples
phpsessionobject-serializationpersistent-object-store

How to keep PHP object alive (or in memory) between AJAX calls


I have the following class definition:

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

What the code above does is the following:

  • The URL http://localhost/datasegmentation is called
  • The view render a select element (modules) with options
  • When the select#modules is changed I sent it's value as part of the URL so the next AJAX call becomes: http://localhost/datasegmentation?table=companies (for example)
  • The indexAction() function then perform what is on the conditional for when $table is not empty or is not null
  • Among all those stuff it tries to generate everything dynamically as you may notice in code.
  • One of those stuff is a dinamic grid ($gridObj) which has a AJAX call to the same indexAction() but without parameters for populate the data after gets rendered
  • After the grid gets rendered at the view it makes the AJAX call and again the indexAction() is called and it skip the conditional for the table because the parameter is not set and tries the second conditional but surprise it fails because the objects that code needs to work are gone.

Having that scenario my questions are:

  • How do I keep the object alives between AJAX calls? Storing in a session var? Any other workaround?
  • If the answer is store them in a session var, is it recommendable? What about the answers on this, this and this among others?
  • How would you handle this?

The problem

  • The second AJAX call is the one adding data to the grid and it relies on the dynamic parameters. This is what I need to solve for make this to work.

I don't know if this is useful at all but this is being tested and develop on top of Zend Framework 1 project using PHP 5.5.x.


Solution

  • How do I keep the object alives between AJAX calls? Storing in a session var? Any other workaround?

    Storing the data in a session var Storing the data in a file, with a client ID (could be a login, random, IP, etc) Storing the data in a database.

    If the answer is store them in a session var, is it recommendable? What about the answers on this, this and this among others?

    If you are storing critical data, use end to end encryption, SSL, HTTPS.

    How would you handle this?

    Using session variables.