Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Doctrine Object in $_SESSION

Hey, ive got a mysterious problem at the moment... i have a login service on my website. I use Doctrine as ORM. When the user entered the right combination for username and password i try to store the retrieved user object in my session for later purpose.

$user = new models\User;
$user = $em->getRepository('models\User')->findOneBy(array(
           'username' => $this->input->post('username'),
           'password' => hash("sha512", $this->input->post('password'))
           ));

   if($user != NULL) {
        session_start();
        $_SESSION['user'] = $user;
    redirect('user');
    }

thats a part from my login

       /**
         * @ManyToOne(targetEntity="Country")
         * @JoinColumn(name="country_id", referencedColumnName="id")
         */
        private $country;

user has different attributes like country, address etc.

so my problem is: if the field Country is null (in the db) for the user the login works fine... but if the user is assigned to a country (country has just id and name) my system fails. after the redirect the is no $user in $_SESSION There is no php error or something like this.. the variable just goes away

would it maybe better just storing the id in session and load the user on every page load?

like image 832
soupdiver Avatar asked Dec 07 '25 04:12

soupdiver


1 Answers

would it maybe better just storing the id in session and load the user on every page load?

Sure. User data may have changed already on next page load, so you obviously need to refresh data from database, not read old data of the user from session.

references:

PHP: Storing 'objects' inside the $_SESSION

like image 199
Vladislav Rastrusny Avatar answered Dec 08 '25 18:12

Vladislav Rastrusny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!