Question

I ll reframe my question based on some research I have done?

I need to store lot of errors separately like $_SESSION['client_error'],$_SESSION['state_error'] etc. According to zend documentation do I have to store it like this for each error?

$client_error = new Zend_Session_Namespace(''client_error);
$state_error = new Zend_Session_Namespace('state_erro'); and so on?

This is my code in the controller. I am storing it as $this->view->state_error_message=$state_error;

After I echo $this->state_error in the view I want to unset it.

Ok here are few more things I tried: In the controller in policyInfoAction:

    session_start();
$error_message = new Zend_Session_Namespace('error_message');
$error_message="TEST";
$this->view->error_message=$error_message;
$this->_redirect('/pdp/client-info/');

In the view in client-info:

session_start();
<?php echo $this->error_message; ?>

This returns nothing.

Ok this is my updated code:

    public function clientInfoAction()
        {

            $errors = new Zend_Session_Namespace('errors');
            // get the error arrays
            $client_errors = (isset($errors->client_error)) ? $errors->client_error : array();
            $state_errors  = (isset($errors->state_error)) ? $errors->state_error : array();
            unset($errors->client_error, $errors->state_error); // delete from the session

            // assign the values to the view
            $this->view->client_errors = $client_errors;
            $this->view->state_errors  = $state_errors;
}


    public function policyInfoAction()
    {

         if (count($arrErrors) > 0)
         {

            // The error array had something in it. There was an error.
            $strError="";

            foreach ($arrErrors as $error)
            {
            $strError="";
            $errors->client_error = array();
            $errors->state_error  = array();


            foreach ($arrErrors as $error)
            {
                $strError .= $error;
                // to add errors to each type:
                $errors->client_error['client_error'] = $strError;
                $errors->client_error[] = $strError;
                $this->_redirect('/pdp/client-info/');


            }
               }
}

When i echo $this->client_errors I get 'Array'

Was it helpful?

Solution

Here is some advice and suggestions that can hopefully get you on the right track.

First, when using Zend_Session and/or Zend_Session_Namespace, you never want to use PHP's session_start() function1. If you start a session with session_start(), and then try to use Zend_Session, it will throw an exception that another session already exists.

Therefore, remove all session_start() calls from your Zend Framework application.

Second, you mentioned you had a lot of messages you need to store, so this may not be the right thing for you, but see the FlashMessenger action helper. This allows you to set a message in a controller, and then access it on the next page request. The messages only live for one page hop, so after the next page load, they are deleted. You can store many messages with the FlashMessenger, but your access to them is not very controlled. You could use multiple flash messengers each in differen namespaces also.

To solve your problem in particular, you could just do something like this:

// in controller that is validating
$errors = new Zend_Session_Namespace('errors');
$errors->client_error = array();
$errors->state_error  = array();

// to add errors to each type:
$errors->client_error['some_error'] = 'You had some error, please try again.';
$errors->client_error['other_error'] = 'Other error occurred.';
$errors->client_error[] = 'Other error, not using a named key';

$errors->state_error[] = MY_STATE_PARSING_0;

What is happening here is we are getting a session namespace called errors creating new properties for client_error and state_error that are both arrays. You don't technically have to use multiple Zend_Session_Namespaces.

Then to clear the messages on the next page load, you can do this:

// from controller again, on the next page load
$errors = new Zend_Session_Namespace('errors');

// get the error arrays
$client_errors = (isset($errors->client_error)) ? $errors->client_error : array();
$state_errors  = (isset($errors->state_error)) ? $errors->state_error : array();

unset($errors->client_error, $errors->state_error); // delete from the session

// assign the values to the view
$this->view->client_errors = $client_errors;
$this->view->state_errors  = $state_errors;

See also the source code for Zend_Controller_Action_Helper_FlashMessenger which can give you some idea on managing data in session namespaces.

OTHER TIPS

I don't know if this will help you or not but here is the code for a controller that just takes an id from a form a gathers data based on that id an assigns that data to the session (to be used throughout the module) and then unsets that data when appropriate. and Never leaves the Index page.

<?php

class Admin_IndexController extends Zend_Controller_Action
{
    //zend_session_namespace('location')
    protected $_session;

    /**
     *set the layout from default to admin for this controller
     */
    public function preDispatch() {

        $this->_helper->layout->setLayout('admin');
    }

    /**
     *initiaize the flashmessenger and assign the _session property
     */
    public function init() {

        if ($this->_helper->FlashMessenger->hasMessages()) {
            $this->view->messages = $this->_helper->FlashMessenger->getMessages();
        }
        //set the session namespace to property for easier access
        $this->_session  = new Zend_Session_Namespace('location');

    }

    /**
     *Set the Station and gather data to be set in the session namespace for use
     * in the rest of the module
     */
    public function indexAction() {

        //get form and pass to view
        $form = new Admin_Form_Station();
        $form->setAction('/admin/index');
        $form->setName('setStation');

        $this->view->station = $this->_session->stationName;
        $this->view->stationComment = $this->_session->stationComment;
        $this->view->form = $form;

        try {
            //get form values from request object
            if ($this->getRequest()->isPost()) {

                if ($form->isValid($this->getRequest()->getPost())) {

                    $data = (object)$form->getValues();

                    //set session variable 'station'
                    $this->_session->station = $data->station;

                    $station = new Application_Model_DbTable_Station();
                    $currentStation = $station->getStation($this->_session->station);
                    $this->_session->stationName    = $currentStation->station;
                    $this->_session->stationComment = $currentStation->comment;

                    //assign array() of stations to session namespace
                    $stations = $station->fetchAllStation();
                    $this->_session->stations = $stations;

                    //assign array() of bidlocations to session namespace
                    $bidLocation  = new Application_Model_DbTable_BidLocation();
                    $bidLocations = $bidLocation->fetchAllBidLocation($this->_stationId);
                    $this->_session->bidLocations = $bidLocations;

                    $this->_redirect($this->getRequest()->getRequestUri());
                }
            }
        } catch (Zend_Exception $e) {

            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->_redirect($this->getRequest()->getRequestUri());
        }
    }

    /**
     *Unset Session values and redirect to the index action
     */
    public function changestationAction() {

        Zend_Session::namespaceGet('location');
        Zend_Session::namespaceUnset('location');

        $this->getHelper('Redirector')->gotoSimple('index');
    }

}

just to be complete i start the session in the bootstrap. On the theory that if I need it great if not no harm.

 protected function _initsession() {
        //start session
        Zend_Session::start();
    }

this is all the view is:

<?php if (!$this->station): ?>
    <div class="span-5 prepend-2">
        <?php echo $this->form ?>
    </div>
    <div class="span-10 prepend-2 last">
        <p style="font-size: 2em">Please select the Station you wish to perform Administration actions on.</p>
    </div>
<?php else: ?>
    <div class="span-19 last">
        <?php echo $this->render('_station.phtml') ?>
    </div>
<?php endif; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top