Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php yii2模型不包含属性错误_Php_Yii2 - Fatal编程技术网

Php yii2模型不包含属性错误

Php yii2模型不包含属性错误,php,yii2,Php,Yii2,在我的TBLTRUKS模型中,我添加了 class TblTrucks extends \yii\db\ActiveRecord { public $rejected_reason; public function rules() { return [ [['rejected_reason'], 'safe'], ] } } 现在在我的控制器中,每当我尝试设置如下值时 $trucks = TblTr

在我的TBLTRUKS模型中,我添加了

class TblTrucks extends \yii\db\ActiveRecord
 {
  public $rejected_reason;

     public function rules()
      {
      return [
        [['rejected_reason'], 'safe'], 
           ]
     }

 }
现在在我的控制器中,每当我尝试设置如下值时

$trucks = TblTrucks::find()->all();

foreach($trucks as $key=>$value){
   $value->rejected_reason = "test here"; 

 }

 return $trucks;
我犯了一个错误

TblTrucks没有名为“拒绝原因”的属性

我哪里会出错

我还尝试在模型中添加字段数组,如

    public function fields()
    {
      $fields = parent::fields();

       $fields['rejected_reason'] =function ($model){
        return $this->rejected_reason;
       };
     return $fields;

    }

只需按以下方式更改您的查询:

$trucks = TblTrucks::find()
      ->select('your_table_name.*,"test here" as rejected_reason') // replace with your table name
      ->all();

只需按以下方式更改您的查询:

$trucks = TblTrucks::find()
      ->select('your_table_name.*,"test here" as rejected_reason') // replace with your table name
      ->all();

您的查询包含数据库中的字段。因此,它不包含公共变量。那么如何向结果中添加新变量。是否可以添加公共变量。您的查询包含来自db的字段。因此,它不包含公共变量。那么我如何向结果中添加新变量。是否可以添加公共变量。