Search code examples
phpyii

Yii: Access Model Constant in Controller


Using Yii Framework, how can I access Model Constant in Controller?

Model.php

...
const STATUS_ACTIVE=1;
...

Controller.php

...
$criteria->condition = 'status='.self::STATUS_ACTIVE;
...

Error:

Fatal error: Undefined class constant 'STATUS_ACTIVE' in ... on line X

Solution

  • In your controller self is the controller's class who does not have this constant. I think you wanted:

    Model::STATUS_ACTIVE
    

    Where Model is the name of the model's class. ie:

    $criteria->condition = 'status='.Model::STATUS_ACTIVE;