yii\db\Query::populate PHP Method

populate() public method

This method is internally used to convert the data fetched from database into the format as required by this query.
public populate ( array $rows ) : array
$rows array the raw query result from database
return array the converted query result
    public function populate($rows)
    {
        if ($this->indexBy === null) {
            return $rows;
        }
        $result = [];
        foreach ($rows as $row) {
            if (is_string($this->indexBy)) {
                $key = $row[$this->indexBy];
            } else {
                $key = call_user_func($this->indexBy, $row);
            }
            $result[$key] = $row;
        }
        return $result;
    }

Usage Example

 /**
  * @param array $rows
  * @return array
  */
 public function populate($rows)
 {
     if ($this->indexBy === null) {
         if ($this->asArray) {
             return $rows;
         }
         $result = [];
         foreach ($rows as $row) {
             $result[] = $this->createObject($row);
         }
         return $result;
     } else {
         return parent::populate($rows);
     }
 }
All Usage Examples Of yii\db\Query::populate