0

I have this controller:

<?php

namespace app\EmpleadosBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;

use app\EmpleadosBundle\Entity\Empleados;
use app\RolesEmpleadosBundle\Entity\RolesEmpleados;

use app\EmpleadosBundle\Service\Util;

session_start();

class DefaultController extends Controller
{
    public function loginAction(){
        //Siempre que abrimos la pagina limpiamos la session
        $this->get('session')->clear();
        return $this->render('EmpleadosBundle:Default:login.html.twig');
    }

    public function conexionAction(Request $request){
        $em = $this->getDoctrine()->getManager();
    $metodo=$request->getMethod();

    if($metodo=='POST'){
        //Obtenemos el n_empleado y la contraseña del formulario
        $nempleado = $request->request->get('nempleado');
        $password = $request->request->get('password');

        //Buscamos en la base de datos para comprobar si hay coincidencia
        $o_empleado = $em->getRepository('EmpleadosBundle:Empleados')->findBy(array(
            'nEmpleado' => $nempleado,
            'password' => $password
        ));

        //Si la consulta devuelve algun registro es que hay coindicendia
        //Almacenamos en session el empleado conectado y redirigimos a la funcion menu principal
        if(count($o_empleado)==1){
            $this->get('session')->set('empleado', $o_empleado);
            //...
    }
}
?>

In this point in conexionAction, i save an object of class EmpleadosBundle:Empleados, and if I print the session in this point is is:

print_r($this->get('session')->get('empleado'));

Array
(
[0] => app\EmpleadosBundle\Entity\empleados Object
    (
        [id:app\EmpleadosBundle\Entity\empleados:private] => 47
        [nEmpleado:app\EmpleadosBundle\Entity\empleados:private] => 0000000002
        [nombre:app\EmpleadosBundle\Entity\empleados:private] => USUARIO DIRECCION FRONT
        [apellidos:app\EmpleadosBundle\Entity\empleados:private] => AAA
        [password:app\EmpleadosBundle\Entity\empleados:private] => 12qwaszx
        [email:app\EmpleadosBundle\Entity\empleados:private] => 
        [fechaAlta:app\EmpleadosBundle\Entity\empleados:private] => DateTime Object
            (
                [date] => 2017-08-29 16:39:50.000000
                [timezone_type] => 3
                [timezone] => UTC
            )

    )

)

And at the end of conexionAction function I redirect to other action, in the same controller:

return $this->redirect($this->generateUrl('menu_principal'));

And in this function the session object change:

Function:

public function menuprincipalAction(){
    $em = $this->getDoctrine()->getManager();

    $empleado_conectado=$this->get('session')->get('empleado');

    if($empleado_conectado[0]!=''){
        $array_roles=array();
        $o_roles_empleado = $em->getRepository('RolesEmpleadosBundle:RolesEmpleados')->findBy(array('empleado'=>$empleado_conectado[0]->getId()));
    //...

Here if I print the session:

print_r($this->get('session')->get('empleado'));

Array
(
[0] => __PHP_Incomplete_Class Object
    (
        [__PHP_Incomplete_Class_Name] => app\EmpleadosBundle\Entity\empleados
        [id:app\EmpleadosBundle\Entity\empleados:private] => 47
        [nEmpleado:app\EmpleadosBundle\Entity\empleados:private] => 0000000002
        [nombre:app\EmpleadosBundle\Entity\empleados:private] => USUARIO DIRECCION FRONT
        [apellidos:app\EmpleadosBundle\Entity\empleados:private] => AAA
        [password:app\EmpleadosBundle\Entity\empleados:private] => 12qwaszx
        [email:app\EmpleadosBundle\Entity\empleados:private] => 
        [fechaAlta:app\EmpleadosBundle\Entity\empleados:private] => DateTime Object
            (
                [date] => 2017-08-29 16:39:50.000000
                [timezone_type] => 3
                [timezone] => UTC
            )

    )

)

It changes to an incomplete class and give a fatal error and can´t use the object.

-- EDIT 1 --

I tryed your solution Steve and it isn´t working, it returns the same error:

I removed the line session_start(),and removin this line is like the session didn´t started.

I will try to explain the process step by step to see if I´m missing something:

1- I call loginAction in Empleados Controller, and clear the session with:

$this->get('session')->clear();

And then I render a view with the login form

2- When I submit the login form I call conexionAction in Empleados Controller, here I check some parameters and if all is ok I save and object of class empleado in session with:

$this->get('session')->set('empleado',serialize($o_empleado)); or $this->get('session')->set('empleado',$o_empleado);

*Note: If I print $this->get('session') here I see the object perfectly.

And after saving the object I redirect to another action in the same controller with:

return $this->redirect($this->generateUrl('menu_principal'));

3- With the previous redirect I call menuprincipalAction in Empleados Controller. And here if I try to print the object I get an array like this:

Array
(
[0] => __PHP_Incomplete_Class Object
    (
        [__PHP_Incomplete_Class_Name] => app\EmpleadosBundle\Entity\empleados
        [id:app\EmpleadosBundle\Entity\empleados:private] => 47
        [nEmpleado:app\EmpleadosBundle\Entity\empleados:private] => 0000000002
        [nombre:app\EmpleadosBundle\Entity\empleados:private] => USUARIO DIRECCION FRONT
        [apellidos:app\EmpleadosBundle\Entity\empleados:private] => AAA
        [password:app\EmpleadosBundle\Entity\empleados:private] => 12qwaszx
        [email:app\EmpleadosBundle\Entity\empleados:private] => 
        [fechaAlta:app\EmpleadosBundle\Entity\empleados:private] => DateTime Object
            (
                [date] => 2017-08-29 16:39:50.000000
                [timezone_type] => 3
                [timezone] => UTC
            )

    )

)

I don´t understant why in the first action the print of the object is correct, but in the second it says incomplete class name

SensacionRC
  • 595
  • 1
  • 13
  • 30
  • https://stackoverflow.com/questions/2010427/php-php-incomplete-class-object-with-my-session-data Symfony will be starting a session before your controller is instantiated – Steve Sep 13 '17 at 18:13

2 Answers2

0

Smfony is almost certainly starting a session before your controller in instantiated, and therefor before the empledos class in loaded.

You can get around this by handling serialization explicitly:

//set
$this->get('session')->set('empleado', serialize($o_empleado));

//get
$empleado_conectado=unserialize($this->get('session')->get('empleado'));
Steve
  • 20,703
  • 5
  • 41
  • 67
  • Ok I will give it a try, so I need to delete the session_start(); line or It is needed??Because you said: "Symfony is almost certainly starting a session before your controller in instantiated" – SensacionRC Sep 14 '17 at 07:36
  • Yes, you should remove that line. – Steve Sep 14 '17 at 08:40
0

At the end, after trying several things, instead of saving an object in session, I save the id of the object, and everywhere I need the object, I get the id from the session:

$this->get('session')->get('empleado_id');

And obtain the objet via:

$em = $this->getDoctrine()->getManager();
$empleado_alta=$em->getRepository('EmpleadosBundle:Empleados')->find($this->get('session')->get('empleado_id'));

With this solution all works perfect.

I think that the redirect in the first action in my question, do something to the session and broke it.

Thanks for all the responses.

SensacionRC
  • 595
  • 1
  • 13
  • 30