在我的一个控制器中,我有一些静态变量和两个动作:
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或更大。我该如何处理这个问题?
答案 0 :(得分:2)
当您在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 here
中会话的更多信息