Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Strange behaviour with session

I have an issue which to me does not make any logical sense. I don't know if it is a bug or something I am completely missing. I have a ControllerBase file which sets a few session variables depending on the url. For instance if it is /fr it will save "fr" to the session. I then have another controller that builds the html href lang tags to display in the layout. Each different language option is saved in the database. This function simply loops through the different options and creates the suitable tag. This function has absolutely no interaction with the user session. I can execute the database query with no issue, but as soon as it starts looping it writes over the previously saved session variable.

Controller Base

<?php

        $language_get = Languages::findFirst(array(
            "region_code=:region_code: AND language_code=:language_code:", 
            "bind" => array(
                "region_code" => $region_code, 
                "language_code" => $language_code
            )
        ));

        $this->session->language = $language_get;

Page Controller

    <?php

    $href_langs = $this->utils->href_langs();

Utils

<?php

    public function href_langs()
    {
        // Session variable still contains initial valid value here.

        $href_languages = Languages::find(array(
            "region_code <> 'gb'",
            "order" => "language_name ASC"
        ));

        // Session variable still contains initial valid value here.

        foreach($href_languages as $href){}

        // Session variable value has changed by here.
    }

I am completely stumped, if anyone can advise or assist, I would be very grateful.



83.4k
Accepted
answer

Try saving an array of the object in session:

$this->session->language = $language_get->toArray();


2.4k
edited Nov '14

Try saving an array of the object in session:

```php $this->session->language = $language_get->toArray(); ```

This works perfect. Thanks Phalcon. Keep up the good work! By the way, we have just launched our site powered by Phalcon and would love to showcase it on the builtwith site.