0

In one of my controllers, I have some static variables and two actions:

class ChooseController extends Controller
{
    private static $wholedata = array();                          

    private static $currentdata = array();                            

    private static $wholenum = 0; 

    private static $currentnum = 0;  

    public function choosefirstAction()
    {
         $company = $this->getUser()->getCompany();
         $em = $this->getDoctrine()->getManager();

         self::$wholedata = $this->getDoctrine()
         ->getRepository('NeejobCompanyBundle:Selected')->findBy(
            array("company" => $company),
            array("job" => 'ASC')
         );

        self::$wholenum = count(self::$wholedata);
        self::$currentdata = array_slice(self::$wholenum, 0, 3);
        self::$currentnum = 3;

        return new response(json_encode(self::$currentdata));
    }

    public function choosemoreAction()
    {
        //... 
        return new response(self::$wholenum);
    }
}

I still have $wholenum = 0, which is supposed to be 3 or larger. How should I handle the problem?

3
  • Could you add the changes that you made to "this data" that's the most important part. Commented Mar 2, 2014 at 3:30
  • I have already copyed my codes in it. I tried some test code, all failed...@Sparkup Commented Mar 2, 2014 at 3:39
  • $wholenum isn't being stored anywhere Commented Mar 2, 2014 at 4:09

1 Answer 1

2

When you send the data in choosefirstAction your class values are no longer set, you need to store them somewhere (i.e. in the session) :

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\JsonResponse;

class ChooseController extends Controller
{
    public function choosefirstAction()
    {
        $company = $this->getUser()->getCompany(); 
        $doc = $this->getDoctrine();


        $wholedata = $doc->getRepository('...Bundle:Selected')->findBy(
            array('company' => $company),
            array('job'     => 'ASC')
        );

        $wholenum = count($wholedata);
        $currentdata = array_slice($wholenum, 0, 3);
        $currentnum  = 3;

        $session = $this->get('session');
        $session->set('wholenum',  $wholenum);

        return new JsonResponse($currentdata);
    }

    public function choosemoreAction()
    {
        $wholenum = $this->get('session')->get('wholenum');

        return new response($wholenum);
    }
}

More on sessions in symfony here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.