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

Undefined Variable Volt Cache -- even if prints successfully

Good Day.

First of all, I want to thank the Phalcon team for this amazing framework.

I am uncountering a weird error in passing variables to Volt. Whenever I try supplying finders with conditions, it always return as expected, but when I pass it to the view, it displays, but there is always either following error message in PHP error logs:

"[Mon Dec 05 15:16:04.014342 2016] [:error] [pid 9440] [client 127.0.0.1:58694] PHP Notice: Undefined variable: user in /var/www/html/cache/_var_www_html_app_views_index_index.volt.php on line 11, referer: https://localhost/charles"

OR

"[Mon Dec 05 15:16:04.014356 2016] [:error] [pid 9440] [client 127.0.0.1:58694] PHP Notice: Trying to get property of non-object in /var/www/html/cache/_var_www_html_app_views_index_index.volt.php on line 11, referer: https://localhost/charles"

// Controller //

public function indexController($username = null) {

$user = User::findFirst(

    [

        "conditions" => "username = :username:",

        "bind" => 

            [

                "username" => $username

            ],

        "columns" => 

            [

                "id", "username"

            ]

    ]

);

$this->view->user = $user;
// even if I use $this->view->setVar("user", $user);

}

// Volt //

{{ user.username }}

or

{{ user["username"] }}

However, when I try to use finders with only the ID used, there is no error.

public function indexController($username = null) {

$user = User::findFirst(1);

$this->view->user = $user;

// even if I use $this->view->setVar("user", $user);

}

// Volt //

{{ user.username }}

or

{{ user["username"] }}

Any help would be appreciated. I know the code still works, either way. However, I will loose my PHP error logs if these errors persists.

*Note: I have also made changes to the router for the index controller to accept parameters without needing to add the additional "index" in the URL.

$di->set("router", function() { $router = new Router(); // Router changes

$router->add("/{username}", [
    "controller" => "index",
    "action" => "index",
]);

$router->add("/{username}/page", [
    "controller" => "index",
    "action" => "index",
]);

$router->add("/{username}/page/{page:\d+}", [
    "controller" => "index",
    "action" => "index",

$router->removeExtraSlashes(true);
return $router;

}, true);

Sorry about the typo. I had to type my code from another laptop. The issue still persists.

findFisrt ?

edited Dec '16

What phalcon version you are using ? Which php version ? Just $user is boolean - means it's false, means there is no such user with $username. Is there such a user with username in database in first place ?

edited Dec '16

Thank you for the help. I am using PHP Version 7.0.8-0ubuntu0.16.04.3 with Phalcon 3.0.2 on my local machine.

Yes, there is. It is being displayed correctly in the view. This is the returned object.

object(Phalcon\Mvc\Model\Row)#104 (2) { ["id"]=> string(1) "4" ["username"]=> string(7) "charles"}

When I try to assign it to the view from the controller like this

// Controller //

$this->view->username = $userData->username;

or

$this->view->setVar("username", $userData->username);

// View //

{{ username }}

i always get those undefined variable errors from volt cache.



1.0k
Accepted
answer
edited Dec '16

It appears adding volt an if statement "is defined" made the errors go away. I am not sure why, since the variable / object is assigned from the controller. Anyways, please see the code below:

{% if username is defined %}

    {{  username }}

{% endif %}

Thank you.

edited Dec '16

when username is null you're not getting an user object, so I think this is the reason of the error in the view

No. If there is nothing returned from the database then object findFirst returns false.

Correct, the user Object is returning false, so it's not returning an object

edited Dec '16

when username is null you're not getting an user object, so I think this is the reason of the error in the view

Actually I also made a variable to be set as true if $user is found like this below

// Controller

$user = User::findFirst(

[

    "conditions" => "username = :username:",

    "bind" => 

        [

            "username" => $username

        ],

    "columns" => 

        [

            "id", "username"

        ]

]

);

if ($user) {

$this->view->found = true;

$this->view->user = $user;

}

// Volt

{% if found is defined %}

{{ user }}

{% endif %}

However, the $user variable still encounters undefined errors. I also noticed that these errors are apparent, even in accessing other controllers. It is only when I directly put $user variable in the "if defined" test, does those errors get addressed.

Try to use setVar. I just have similar code and:

$user = User::findFirst([
// whatever here
]);
$this->view->setVar('user', $user);

// view
{% if user %}
{{ user.username }}
{% endif %}

Is pretty much enough

edited Dec '16

Although the topic is solved IMHO it's good to know what was the problem

"[Mon Dec 05 15:16:04.014342 2016] [:error] [pid 9440] [client 127.0.0.1:58694] PHP Notice: Undefined variable: user in /var/www/html/cache/varwwwhtmlappviewsindex_index.volt.php on line 11, referer: https://localhost/charles"

this err is because you are not defining a var in controller or you are not putting this var properly into the view

OR

"[Mon Dec 05 15:16:04.014356 2016] [:error] [pid 9440] [client 127.0.0.1:58694] PHP Notice: Trying to get property of non-object in /var/www/html/cache/varwwwhtmlappviewsindex_index.volt.php on line 11, referer: https://localhost/charles"

this err is because you are putting the var into view, but it's not an object (problably boolean - false). You should var_dump it to be sure

Only step-by-step analysis can make you smarter =)