Reputation: 401
im trying to make a simple cart in symfony2, but the documentation about session is very limited and the only example i have found are about user.
from what i understand in watching videos is that have to make this steps:
1-make sure to have a session array, if not declare a new session array 2-add variables to the session array via array_push(); 3-display session
this is my code so far:
public function sessiontestAction(Request $request)
{
$session = $request->getSession();
if(!$session)
{
$session->set('producto');
}
$em = $this->getDoctrine()->getManager();
$producto = $em->getRepository('savaInventarioBundle:TblProductos')->find(29);
if(!$producto){
throw $this->createNotFoundException('no se encontro el producto');
}
array_push($session, $producto);
return $this->render('savaInventarioBundle:Catalogo:sessiontest.html.twig',
array('productos'=> $session));
}
my output just throws 1 product instead more then one every time i invoke the function, also it shows this error "Warning: array_push() expects parameter 1 to be array, object given i"
Upvotes: 0
Views: 3344
Reputation: 401
so after some testing i solved my question. if you ever want to use array_push() to manage session in symfony 2, you can do it like this.
symfony2 manages session and you shouldnt do it with $_SESSION, this is how i can push arrays in the session.
public function sessiontestAction(Request $request) {
$productos = array();
// $session = $request->getSession();
$session = $this->getRequest()->getSession();
//check if the session have products
if ($session->has('producto')) {
$productos = $session->get('producto');
array_push($productos, "tomate", "lechuga");
$session->set('producto', $productos);
} //if it doesnt create the session and push a array for testing
else{
$test = array("orange", "banana");
$session->set('producto', $test);
}
//in order to pass an array from the session, you have to set it on a new array. $productos = $session->get('producto'); return $this->render('savaInventarioBundle:Catalogo:sessiontest.html.twig', array('productos' => $productos)); }
Upvotes: 2
Reputation: 410
get the session like this: $session = $request->getSession();
and set the parameters in session like this: $session->set('session_var_name', $var);
and get the parameters in session like this: $request->get('session_var_name');
I hope this help you!
Upvotes: 0
Reputation: 4210
$request->getSession()
returns an object (instance of Session which implements SessionInterface), array_push function receives an array as first argument (array_push (array &$array , mixed $value1 [, mixed $... ])
), of course you can't use array_push function here.
I think the solution will be creating an array, set this array to session, second time retreive it back from session modify it and store it back into session, for example:
$session = $request->getSession();
$myArray = array(
FIRST_ELEMENT
);
$session->set('cartElements', $myArray);
....
$cartElements = $session->get('cartElements');
array_push($cartElements, 'SECOND_ELEMENT');
$session->set('cartElements', $cartElements);
....
Upvotes: 1