Yii hasAttribute 和自定义字段

Luc*_*nto 1 php yii

如何使用hasAttribute自定义字段?

代码:

if ($model->hasAttribute($attribute)) {
    ...
} else {
    $this->_sendResponse(400, 'Parameter \''.$attribute.'\' is not supported.');
}
Run Code Online (Sandbox Code Playgroud)

例子

模型.php

class Model extends CActiveRecord
{

public $customField;
...
Run Code Online (Sandbox Code Playgroud)

代码:

$model = new Model;
$model->hasAttribute('customField'); // Returns False.
Run Code Online (Sandbox Code Playgroud)

soj*_*oju 5

你应该简单地使用:

if (property_exists('Model', $attribute)) {
Run Code Online (Sandbox Code Playgroud)

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

hasAttribute只会检查数据库属性。