Skip to content
Advertisement

Yii hasAttribute and Custom Fields

How can I use hasAttribute with Custom Fields?

Code:

if ($model->hasAttribute($attribute)) {
    ...
} else {
    $this->_sendResponse(400, 'Parameter ''.$attribute.'' is not supported.');
}

EXAMPLE

Model.php

class Model extends CActiveRecord
{

public $customField;
...

Code:

$model = new Model;
$model->hasAttribute('customField'); // Returns False.

Advertisement

Answer

You should simply use :

if (property_exists('Model', $attribute)) {

https://www.php.net/manual/en/function.property-exists.php

hasAttribute will only check for db attributes.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement