Bluz\Db\Relations::fetch PHP Method

fetch() public static method

Fetch by Divider
public static fetch ( array $input ) : array
$input array
return array
    public static function fetch($input)
    {
        $output = [];
        $map = [];
        foreach ($input as $i => $row) {
            $model = '';
            foreach ($row as $key => $value) {
                if (strpos($key, '__') === 0) {
                    $model = substr($key, 2);
                    continue;
                }
                $map[$i][$model][$key] = $value;
            }
            foreach ($map[$i] as $model => &$data) {
                $data = self::createRow($model, $data);
            }
            $output[] = $map;
        }
        return $output;
    }

Usage Example

Esempio n. 1
0
 /**
  * Returns an array of linked objects containing the result set
  *
  * @api
  * @param string $sql <p>
  *  "SELECT '__users', u.*, '__users_profile', up.*
  *   FROM users u
  *   LEFT JOIN users_profile up ON up.userId = u.id"
  *   WHERE u.name = :name
  *  </p>
  * @param array $params <p>
  *  array (':name' => 'John')
  * </p>
  * @return array
  */
 public function fetchRelations($sql, $params = array())
 {
     $stmt = $this->prepare($sql, $params);
     $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
     // prepare results
     $result = Relations::fetch($result);
     $stmt->closeCursor();
     $this->ok();
     return $result;
 }