【问题标题】:yii activerecord return objectyii activerecord 返回对象
【发布时间】:2017-08-20 03:46:46
【问题描述】:

我在 MySQL 数据库的基本模板中使用 yii2
为什么此代码返回一个对象,而不是从数据库中选择的记录数组 当我使用 var_damp($rooms) 时,输出似乎是一个对象,而不是数组格式的选定记录数组; 任何机构都可以提供帮助

 public function actionIndexFiltered()
    {
        $query = Room::find();

        $searchFilter = [
            'floor' => ['operator' => '', 'value' => ''], 
            'room_number' => ['operator' => '', 'value' => ''], 
            'price_per_day' => ['operator' => '', 'value' => ''], 
        ];

        if(isset($_POST['SearchFilter']))
        {
            $fieldsList = ['floor', 'room_number', 'price_per_day'];

            foreach($fieldsList as $field)
            {
                $fieldOperator = $_POST['SearchFilter'][$field]['operator'];
                $fieldValue = $_POST['SearchFilter'][$field]['value'];

                $searchFilter[$field] = ['operator' => $fieldOperator, 'value' => $fieldValue];

                if( $fieldValue != '' )
                {
                    $temp1=$query->andWhere([$fieldOperator, $field, $fieldValue]);
                }
            }
        }
        $room1=$temp1->all();
        $rooms = $query;

        return $this->render('indexFiltered', [ 'rooms' => $rooms, 'searchFilter' => $searchFilter,'room1'=>$room1 ]);



    }

输出是这样的,这表明此代码返回一个查询对象,而不是数据库上的查询执行数组, 所以我使用 var_dump 函数检查了代码,结果是它返回的是一个对象而不是数组

 object(yii\db\ActiveQuery)[70]
      public 'sql' => null
      public 'on' => null
      public 'joinWith' => null
      public 'select' => null
      public 'selectOption' => null
      public 'distinct' => null
      public 'from' => 
        array (size=1)
          0 => string 'room' (length=4)
      public 'groupBy' => null
      public 'join' => null
      public 'having' => null
      public 'union' => null
      public 'params' => 
        array (size=0)
          empty
      private '_events' (yii\base\Component) => 
        array (size=0)
          empty
      private '_behaviors' (yii\base\Component) => 
        array (size=0)
          empty
      public 'where' => 
        array (size=3)
          0 => string '=' (length=1)
          1 => string 'room_number' (length=11)
          2 => string '3' (length=1)
      public 'limit' => null
      public 'offset' => null
      public 'orderBy' => null
      public 'indexBy' => null
      public 'emulateExecution' => boolean false
      public 'modelClass' => string 'app\models\Room' (length=15)
      public 'with' => null
      public 'asArray' => null
      public 'multiple' => null
      public 'primaryModel' => null
      public 'link' => null
      public 'via' => null
      public 'inverseOf' => null

【问题讨论】:

    标签: arrays object activerecord yii2


    【解决方案1】:

    此代码返回一个 dataProvider .. 为获取模型/活动记录的代码取别名

    $query = Room::find();
    

    获取你可以使用的所有模型

    $roomModels= Room::find()->all(); 
    

    结果是类 Room 的对象(模型)的集合

    如果你需要一个数组,你可以使用

    $roomArray = Room::find()->asArray()->all(); 
    

    【讨论】:

      【解决方案2】:

      在您的代码中,$temp1$query 相同。 andWhere() 方法返回 $this 这意味着

      $temp1 = $query->andWhere([$fieldOperator, $field, $fieldValue]);
      

      使$temp1$query 相同,后者是yii\db\ActiveQuery 的对象。

      现在你这样称呼:

      $room1 = $temp1->all(); // the same as $room1 = $query->all();
      

      this 持有 Room 对象数组,而

      $rooms = $query;
      

      只是另一个不必要的分配,因为$rooms$query 相同,后者是yii\db\ActiveQuery 的对象。

      【讨论】:

        【解决方案3】:

        最后我发现这个问题的原因是 我必须调用 ActiveRecord 类的 asArray() 成员函数来生成数组输出 $room1=$temp1->all(); 将更改为 $room1=$temp1->asArray()->all();

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-10
          相关资源
          最近更新 更多