Doctrine\DBAL\Connection::convertToPHPValue PHP Method

convertToPHPValue() public method

Converts a given value to its PHP representation according to the conversion rules of a specific DBAL mapping type.
public convertToPHPValue ( mixed $value, string $type ) : mixed
$value mixed The value to convert.
$type string The name of the DBAL mapping type.
return mixed The converted type.
    public function convertToPHPValue($value, $type)
    {
        return Type::getType($type)->convertToPHPValue($value, $this->getDatabasePlatform());
    }

Usage Example

 /**
  * read $columns from $table_name
  * @param array $columns
  * @param array $filters
  * @param array $limit
  * @param array $order_by
  * @param string $return_type
  * @return array:
  */
 public function read(array $columns = [], array $filters = [], array $limit = [], array $order_by = [], $return_type = self::READ_RETURN_COMPLEX)
 {
     $return = [];
     $alias_mapping = [];
     $qb = $this->conn->createQueryBuilder();
     if (empty($columns)) {
         $columns = $this->getColumnNames();
     }
     foreach ($columns as $column) {
         $column_name = null;
         $alias = null;
         $expr = [];
         if (is_string($column)) {
             $column_name = $alias = $column;
             if (strpos($alias, ':') !== false) {
                 $alias = explode(':', $alias);
                 $alias = array_pop($alias);
             }
         } elseif (is_array($column)) {
             list($column_name, $alias) = $column;
         }
         if (strpos($column_name, ':') !== false) {
             $expr = explode(':', $column_name);
             $column_name = array_pop($expr);
         }
         $alias_mapping[$alias] = $column_name;
         if ($this->getColumn($column_name) === null) {
             throw Schema\SchemaException::columnDoesNotExist($column_name, $this->table_name);
         }
         $column_expr = $this->conn->quoteIdentifier($column_name);
         $expr = array_reverse($expr);
         foreach ($expr as $ex) {
             if (in_array($ex, ['max', 'min', 'avg', 'count', 'sum', 'lower', 'upper'])) {
                 $column_expr = call_user_func([$this->conn->getDatabasePlatform(), 'get' . ucfirst($ex) . 'Expression'], $column_expr);
             } else {
                 throw QueryBuilderException::expressionTypeDoesNotExist($ex);
             }
         }
         if ($column_name !== $alias || !empty($expr)) {
             $column_expr .= ' AS ' . $this->conn->quoteIdentifier($alias);
         }
         $qb->addSelect($column_expr);
     }
     $qb->from($this->quoted_table_name);
     $this->buildWhere($qb, $filters)->buildOrderBy($qb, $order_by)->buildLimit($qb, $limit);
     $res = $qb->execute();
     $return = $res->fetchAll(\PDO::FETCH_ASSOC);
     if (in_array($return_type, [self::READ_RETURN_SIMPLE, self::READ_RETURN_COMPLEX])) {
         foreach ($return as $index => $row) {
             foreach ($row as $column => $value) {
                 if ($return_type === self::READ_RETURN_SIMPLE && !$this->isSimpleType($alias_mapping[$column])) {
                     continue;
                 }
                 $return[$index][$column] = $this->conn->convertToPHPValue($value, $this->column_types[$alias_mapping[$column]]);
             }
         }
     }
     return $return;
 }
All Usage Examples Of Doctrine\DBAL\Connection::convertToPHPValue