/** * @param int $depth * @return array */ public function toArray($depth = 1) { $result = []; foreach (array_merge($this->metadata->getLocalFields(), $this->metadata->getVirtualFields()) as $field) { /** @var Field\Base $field */ if (!$field->protected) { $k = $field->name; if ($field->isRelation()) { if ($field instanceof SingleRelation) { if ($depth > 1) { if (isset($this->relationDataCache[$k])) { $result[$k] = $this->getLazyRelation($field)->toArray($depth - 1); } else { $result[$k] = isset($this->data[$field->db_column]) ? $this->data[$field->db_column] : null; } } else { $result[$field->db_column] = isset($this->data[$field->db_column]) ? $this->data[$field->db_column] : null; } } } else { $result[$k] = $this->__get($k); } } } return $result; }