Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

var if defined = true, but not found when i try to dump it #28

Open
AlexGavrilov939 opened this issue Aug 1, 2014 · 3 comments
Open
Assignees

Comments

@AlexGavrilov939
Copy link

user have a custom property (e,g, Yii::app()->user->color).
how can i use it from twig view ?
I tried this design
{% if app.user.color is defined %}
test msg
{{app.user.color}}
{% endif %}

if returns true but property color not found.
how can this happen?

@samdark
Copy link
Member

samdark commented Aug 1, 2014

What do you mean by property color not found?

@samdark samdark self-assigned this Aug 1, 2014
@resurtm
Copy link
Contributor

resurtm commented Aug 2, 2014

What's your configuration file? How your WebUser component and User model looks like?

@AlexGavrilov939
Copy link
Author

ok, tell the order)
i have users model
class UsersModel extends CActiveRecord {

public $id;
public $username;
public $password;
public $firstName;
public $lastName;
public $rank;
public $sinceDate;

public function tableName()
{
    return 'users';
}

public static function model($className=__CLASS__)
{
    return parent::model($className);
}

}

and UserIdentity component here:
class UserIdentity extends CUserIdentity
{
private $_id;
public $user;

public function authenticate()
{
    $this->user = UsersModel::model()->findByAttributes(array('username' => $this->username));
    if($this->user === null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    else if($this->user->password !== $this->password)
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->_id = $this->user->username;
        $this->setState('firstName', $this->user->firstName);
        $this->setState('lastName', $this->user->lastName);
        $this->setState('rank', $this->user->rank);
        $this->setState('sinceDate', $this->user->sinceDate);
        $this->errorCode=self::ERROR_NONE;
    }
    return !$this->errorCode;
}

public function getId()
{
    return $this->_id;
}

}

so I have custom properties (firstName, lastName, rank, sinceDate)
I have no problem to use them like this controller:

// of course, this "if" is true
if (isset(Yii::app()->user->firstName)) {
var_dump(Yii::app()->user->firstName);
} else {
var_dump('you do not have such a property');
}

or:
// it also works well and I get the "else" result
if (isset(Yii::app()->user->blablaProperty)) {
var_dump(Yii::app()->user->firstName);
} else {
var_dump('you do not have such a property');
}

I'm trying to play a similar logic in TWIG templates but it does not work:
{% if app.user.blabla is defined %}
{{ app.user.blabla }}
{% endif %}

(expect that this condition is not satisfied because the property does not exist)
but it's true and I get an error:
An exception has been thrown during the rendering of a template ("In class WebUser and his behavior does not find a method or closure named" blabla ".") In "layouts / backend / includes / header.tpl".

in configs i have:
"user" => array(
"class" => "WebUser"
)

my class WebUser until empty and simply inherits CWebUser

why the "IS DEFINED" is true if it is not really so?
p.s. I am sorry if at first misled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants