We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Retrieving data only once in NAV Tab

Hi,

I would like to retrieve the data in each NAV tab only once till saved or discarded. I setup session variable to indicate that tab data is retrieved and should not be retrieved when tab is selected again for editing but the value of this variable not retained across each tab click. The EmployeeForm gets retrieved every time. I am not sure why this is happening. Is that the way its supposed to work. Here is the code:

class EmployeeController extends IndexController
{
    public function initialize()
    {
        $this->session->set("tabRetrieved", 0);
        parent::initialize();
    ...
    }

    public function editAction($hr_emp_iid)
    {
        $tabRetrieved = $this->session->get("tabRetrieved") ;

        echo '1 TabR Value: '. $this->session->get("tabRetrieved") ."<br/>" ;
        if (!$tabRetrieved) {
            echo 'tabRetrieved : ' . $tabRetrieved. ' . "\r\n" ;
            $employee = HrEmployeeM::findFirst(....);
            if (!$employee) {
                $this->flash->error("Employee was not found");
                return $this->dispatcher->forward(array(
                    'action' => 'index'
                ));
            }
            $this->view->form = new EmployeeForm($employee, array( 'edit' => true ));
            $this->session->set("tabRetrieved", 1);
            echo '2 TabR Value: '. $this->session->get("tabRetrieved")."<br/>" ;

.....

I tried using $this->persistent->tabRetrieved also but same result. Value of these variables doesn't seem to be retained in both cases or I am doing something wrong. Is this the correct way to code?

Amal



17.5k

You could cache the data, instead. First, check if the key exists. If the key exists, return the data. If the key doesn't exist, fetch the data and store it in cache with the specified key.



16.3k

Thanks @Zach. I will try that but any idea why session variables are not being retained across actions. Aren't they supposed to? The value of $this->session->get("tabRetrieved") should be 1 till I set it to 0 or I logout - right.



17.5k
edited Aug '16

Try removing in the initialize() method:

$this->session->set("tabRetrieved", 0);

Inside of the editAction method, try

public function editAction($hr_emp_iid) {
    $key = "tabRetrieved";

    if ($this->session->has($key) && $this->session->get($key) === 1) {
        return;
    }

    $employee = HrEmployeeM::findFirst(....);
    if (!$employee) {
        $this->flash->error("Employee was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }
    }
    $this->view->form = new EmployeeForm($employee, array( 'edit' => true ));
    $this->session->set($key,1);
}


16.3k
edited Sep '16

@Zach, your suggestion was right and I studied the whole Controller doc again. Realized, setting session value in initialize is the issue. Thanks for your effort. I still have a question though. I am unable to retain the values in the form, which is my core issue. I checked many posts, viz.

Still unable to get it going. I want to retain values on the tab even if the user clicks on other tabs and comes back. I was trying to use a static variable to not execute the findFirst as I wrote above and I used ['class': 'form-control'] for all form fields and it seemed to work last week for a while but I changed a few lines in Controller and ever since it has not worked. I am unable to get it working now. Is it possible to retain values in a form inside a tab without a submit? I would appreciate if you have any suggestions.



17.5k

@Amal - Sorry, can't help you there. I use Angular templating in the UI. I don't use volt or forms through Phalcon.