Mapping your object to an array

The method "toArray" convert a record (instance of PorteRecord) or a list of records (instance of PorteIterator) into a single array.

Default options may be set for a given type of record by filling the model table configuration with the property "to_array".

Available options

The toArray method accept an optionalparameter in the form of an array to alter its behavior. The following properties are available:

Simple usage

In its simplest form, this method return the current object as an array. Note that all the values stored in the database columns are stored under the attributes key. If the object is not loaded with its relationships, the associations key in an empty array

Exemples on using the toArray method in its simplest form:


$user = new User();
$user->load(34);
$userArray = $user->toArray();
print_r($userArray);

/* will print:
Array
(
  [attributes] => Array
    (
      [id] => 34,
      [lastname] => 'my lastname',
      [firstname] => 'my firstname'
    )
  [associations] => Array
    (
    )
)
*/

Working with associations

The returned array will reflect the relationships loaded in the current object..

Exemples on using the toArray method with loaded associations:


$file = new File();
$file->load(56);
$file->getFormat();
$file->getUsers();
$fileArray = $file->toArray();
print_r($fileArray);

/* will print:
Array
(
  [id] => 56,
  [name] => my file
  [format] => Array
    (
      [id] => 61
      [name] => my format
    )
  [users] => Array
    (
      [0] => Array
        (
          [id] => 34
          [lastname] => my lastname
          [firstname] => my firstname
        )
      [1] => Array
        (
          [id] => 98
          [lastname] => a lastname
          [firstname] => a firstname
        )
    )
)
*/

You can use the "extended" option in case you wish to split the foreign keys values stored in the database from the loaded objects. This option create to keys "attributes" and "associations" which reflect the internal storage of the records properties.

Method toArray with the "extended" option:


$file = new File();
$file->load(56);
$file->getFormat();
$file->getUsers();
$fileArray = $file->toArray();
print_r($fileArray);

/* will print:
Array
(
  [attributes] => Array
    (
      [id] => 56,
      [name] => my file
    )
  [associations] => Array
    (
      [format] => Array
        (
          [attributes] => Array
            (
              [id] => 61
              [name] => my format
            )
          [associations] => Array
            (
            )
        )
      [users] => Array
        (
          [0] => Array
            (
              [attributes] => Array
                (
                  [id] => 34
                  [lastname] => my lastname
                  [firstname] => my firstname
                )
              [associations] => Array
                (
                )
            )
          [1] => Array
            (
              [attributes] => Array
                (
                  [id] => 98
                  [lastname] => a lastname
                  [firstname] => a firstname
                )
              [associations] => Array
                (
                )
            )
        )
    )
)
*/

Open Source Object Relational Mapping in PHP

Download Porte 0.2.1