lithium\data\Entity::to PHP Method

to() public method

Converts the data in the record set to a different format, i.e. an array.
public to ( string $format, array $options = [] ) : mixed
$format string Currently only `array`.
$options array Options for converting: - `'indexed'` _boolean_: Allows to control how converted data of nested collections is keyed. When set to `true` will force indexed conversion of nested collection data. By default `false` which will only index the root level.
return mixed
    public function to($format, array $options = array())
    {
        $defaults = array('handlers' => array());
        $options += $defaults;
        $options['handlers'] += $this->_handlers;
        switch ($format) {
            case 'array':
                $data = $this->_updated;
                $rel = array_map(function ($obj) {
                    return $obj->data();
                }, $this->_relationships);
                $data = $rel + $data;
                $options['indexed'] = isset($options['indexed']) ? $options['indexed'] : false;
                $result = Collection::toArray($data, $options);
                break;
            case 'string':
                $result = $this->__toString();
                break;
            default:
                $result = $this;
                break;
        }
        return $result;
    }

Usage Example

Example #1
0
 /**
  * Converts a `Record` object to another specified format.
  *
  * @param string $format The format used by default is `array`
  * @param array $options
  * @return mixed
  */
 public function to($format, array $options = array())
 {
     $defaults = array('handlers' => array('stdClass' => function ($item) {
         return $item;
     }));
     $options += $defaults;
     return parent::to($format, $options);
 }
All Usage Examples Of lithium\data\Entity::to