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:
http://localhost/datasegmentation
is calledmodules
) with optionsselect#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)indexAction()
function then perform what is on the conditional for when $table
is not empty or is not null$gridObj
) which has a AJAX call to the same indexAction()
but without parameters for populate the data after gets renderedindexAction()
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:
The problem
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.
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.