Symfony2控制器静态变量

诺亚

在我的一个控制器中,我有一些静态变量和两个动作:

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);
    }
}

我仍然有$wholenum = 0,应该是3或更大。我应该如何处理这个问题?

火花

当您在choosefirstAction类中发送数据时,不再设置值,则需要将它们存储在某个位置(即在会话中):

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);
    }
}

更多关于symfony的会议在这里

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章