public function toArray($keyPrefix= '', $rawValues= false, $excludeLazy= false, $includeRelated= false) {
        $objArray= null;
        if (is_array($this->_fields)) {
            $keys= array_keys($this->_fields);
            if (!$excludeLazy && $this->isLazy()) {
                $this->_loadFieldData($this->_lazy);
            }
            foreach ($keys as $key) {
                if (!$excludeLazy || !$this->isLazy($key)) {
                    $objArray[$keyPrefix . $key]= $rawValues ? $this->_fields[$key] : $this->get($key);
                }
            }
        }
        if (!empty($includeRelated)) {
            $graph = null;
            if (is_int($includeRelated) && $includeRelated > 0) {
                $graph = $this->xpdo->getGraph($this->_class, $includeRelated);
            } elseif (is_string($includeRelated)) {
                $graph = $this->xpdo->fromJSON($includeRelated);
            } elseif (is_array($includeRelated)) {
                $graph = $includeRelated;
            }
            if ($includeRelated === true || is_array($graph)) {
                foreach ($this->_relatedObjects as $alias => $branch) {
                    if ($includeRelated === true || array_key_exists($alias, $graph)) {
                        if (is_array($branch)){
                            /** @var xPDOObject $obj */
                            foreach($branch as $pk => $obj){
                                $objArray[$alias][$pk] = $obj->toArray($keyPrefix, $rawValues, $excludeLazy, $includeRelated === true ? true : $graph[$alias]);
                            }
                        } elseif ($branch instanceof xPDOObject) {
                            $objArray[$alias] = $branch->toArray($keyPrefix, $rawValues, $excludeLazy, $includeRelated === true ? true : $graph[$alias]);
                        }
                    }
                }
            }
        }
        return $objArray;
    }