yii\mongodb\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

Exemplo n.º 1
0
 /**
  * Converts the raw query results into the format as specified by this query.
  * This method is internally used to convert the data fetched from database
  * into the format as required by this query.
  * @param array $rows the raw query result from database
  * @return array the converted query result
  */
 public function populate($rows)
 {
     $result = [];
     foreach ($rows as $file) {
         $row = $file->file;
         $row['file'] = $file;
         $result[] = $row;
     }
     return parent::populate($result);
 }