4

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.

Community
  • 1
  • 1
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • 2
    You don't. Sticking an object into a session serializes it to textual format and then deserializes it when you want to work with it. There's no way to keep an object alive because that goes against the design of PHP. As with everything, there *are* ways to do this but the question is - what are you solving? This is a clear XY problem, and without the reason why you're after this perceived solution - there's no way to offer proper help. – N.B. Oct 31 '16 at 22:36
  • @N.B. read again the updated OP where says *Why?* that's what I am trying to figure it out – ReynierPM Oct 31 '16 at 22:40
  • That's not why. That's because. What would an object that's stored in memory aid you with? There **has** to be a reason why you think that something from memory which persists through request solves something you're having issues with. There are reasons why PHP doesn't behave like this (there are edge cases), and there has to be a valid reason why you want to coerce it to do so. What's wrong with saving required data so you can create your object when needed? – N.B. Oct 31 '16 at 22:41
  • @N.B. Look at the second condition, notice that I need to use dynamic values generated during the first call so on the second they are gone – ReynierPM Oct 31 '16 at 22:42
  • 2
    So save the values and use them to create whatever object you need. Why do you need the object **from memory**? What's wrong with saving sufficient data to session that lets you create whatever object you need? You're overthinking this. – N.B. Oct 31 '16 at 22:43
  • @N.B. maybe, I will try tomorrow and will let you know but I was asking because people say is not recommended – ReynierPM Oct 31 '16 at 22:46
  • 2
    What's not recommended? To use values you need to create programming constructs that solve your problem? Why is that not recommended? Because it makes sense? Why listen to idiots? What's the suggested alternative according to those people? To use PHP for what it's not made for? Even if you were able to store objects in memory, you'd trade so much off for such a feature that doesn't even make sense in most cases.. Question everything. Even my comments. :) – N.B. Oct 31 '16 at 22:49
  • 1
    You might look into some kind of caching mechanism like memcache or redis to take advantage of in-memory storage. You can probably also use the serialize and unserialize function php has to serialize an object back/forth. Keep in mind that the serialize/unserialize will reconstruct your object from a string, meaning you will lose any references (file pointers, database connections, etc) used within your class. – xangxiong Nov 05 '16 at 22:26

1 Answers1

2

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.