We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



98.9k
Accepted
answer

Try saving an array of the object in session:

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


4.6k
edited Nov '14

Try saving an array of the object in session:

$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.