Session data is lost between two different calls on the same controller, why?
I have a "mixed" project with Symfony 3.2.6 (SF) and Zend Framework 1 (ZF). Sometimes I need to move from SF to ZF because not all of the code has been refactored | rewritten in SF. I am currently having problems because ZF and SF store things differently in the session. So far, my solution has been to look for an SF Request
or for a boolean object . Take a look at the code below which belongs to the ZF controller:
abstract class MainInitController extends Zend_Controller_Action
{
...
}
class ExportfileController extends MainInitController
{
public static function registerZendDbSelect($reportId, $select, $options = [])
{
$sess = new Zend_Session_Namespace('exportfile');
$reports = $sess->reports;
...
if (isset($options['request']) && $options['request'] instanceof Request) {
$session = new Session();
$session->set('reports', $reports);
return $shaKey;
}
$sess->reports = $reports;
return $shaKey;
}
public static function fetchQuery($shaKey, $sf = false)
{
if ($sf) {
$session = new Session();
$reportS = $session->get('reports');
} else {
$sess = new Zend_Session_Namespace('exportfile');
$reportS = $sess->reports;
}
...
return $report;
}
public function gridexportAction()
{
$shaKey = $this->getRequest()->getParam('key');
$sf = (bool) $this->getRequest()->getParam('sf');
...
$report = self::fetchQuery($shaKey, $sf);
...
}
}
The method registerZendDbSelect()
is executed on page load. This method belongs to the ZF class, but it is called from the Symfony controller:
class AgreementEditorGrid extends DynamicGrid
{
...
protected function getExportLink()
{
$reportId = 'agreement_list';
$exportQuery = clone $this->mappedSelect;
$exportQuery->limit();
$blocked = ['actions'];
$extra_fields = [];
$options = [
'report_name' => 'Agreement',
'blocked_fields' => $blocked,
'extra_fields_selects_value' => $extra_fields,
'request' => $this->request,
];
$exportKey = ExportfileController::registerZendDbSelect($reportId, $exportQuery, $options);
return "/exportfile/gridexport/key/{$exportKey}/sf/1";
}
}
I checked the data stored in the session by adding two sentences dump()
and it looks like this:
(new \Symfony\Component\VarDumper\VarDumper())->dump($session->get('reports'));
(new \Symfony\Component\VarDumper\VarDumper())->dump($session->getId());
array:1 [
"48362e9fcfbe08d81ddcf534cd3fe19dfc9c8d7f" => array:4 [
"query" => Query {#522
-origQuery: "SELECT `agreement`.`agreement_id` AS `agreement.agreement_id`, `agreement`.`AgreementNumber` AS `agreement.AgreementNumber`, `agreement`.`StartDate` AS `agreement.StartDate`, `agreement`.`EndDate` AS `agreement.EndDate`, `agreement`.`AgreementTypeID` AS `agreement.AgreementTypeID`, `agreement`.`CFProgramLevelID` AS `agreement.CFProgramLevelID`, `agreement`.`TAMFlag` AS `agreement.TAMFlag`, `agreement`.`ParentAgreementNumber` AS `agreement.ParentAgreementNumber`, `agreement`.`DistributorID` AS `agreement.DistributorID`, `agreement`.`CustomerSiteID` AS `agreement.CustomerSiteID`, `agreement`.`Source` AS `agreement.Source`, `agreement`.`Status` AS `agreement.Status` FROM `agreement`"
-select: null
-query: false
-mapFunction: null
-paginationCount: null
-paginationOffset: null
-pagination: false
-curRow: null
-curKey: 0
}
...
Here is the complete link file.
The method gridexportAction()
is called when I click on a link that is on the same page it was called on registerZendDbSelect()
.
This is how the method is called fetchQuery()
. I checked the content reports
by dumping its content - inside the method fetchQuery()
, but the surprise is empty even if session_id
it hasn't changed:
$session = new Session();
$reportS = $session->get('reports');
(new \Symfony\Component\VarDumper\VarDumper())->dump($session->get('reports'));
(new \Symfony\Component\VarDumper\VarDumper())->dump($session->getId());
null
"49tig5ghqevsd7t0qqu3hkm7l0"
What's wrong here? Am I missing some of the basics of session management in Symfony or PHP or the web?
source to share
No one has answered this question yet
Check out similar questions: