/** * Do not call directly! Call {@see Entity::toArray()} instead. * @param IEntity * @param int * @param int */ public static function toArray(IEntity $entity, $mode = self::AS_IS, $deep = 0) { if ($mode === NULL) { $mode = self::AS_IS; } $result = array('id' => isset($entity->id) ? $entity->id : NULL); $rules = MetaData::getEntityRules(get_class($entity), $entity->getModel(false)); foreach ($rules as $name => $rule) { if ($name === 'id') { continue; } if (isset($rule['get'])) { $result[$name] = $entity->__get($name); if ($result[$name] instanceof IEntity and !($mode & self::ENTITY_AS_IS)) { if ($mode & self::ENTITY_AS_ID) { $result[$name] = $result[$name]->id; } else { if ($mode & self::ENTITY_AS_ARRAY) { $result[$name] = $deep > static::$maxDeep ? NULL : EntityToArray::toArray($result[$name], $mode, $deep + 1); } else { throw new EntityToArrayNoModeException(array($entity, true, false)); } } } else { if ($result[$name] instanceof IRelationship and !($mode & self::RELATIONSHIP_AS_IS)) { if ($deep > static::$maxDeep) { $result[$name] = NULL; } else { $arr = array(); foreach ($result[$name] as $e) { if ($mode & self::RELATIONSHIP_AS_ARRAY_OF_ID) { $arr[] = $e->id; } else { if ($mode & self::RELATIONSHIP_AS_ARRAY_OF_ARRAY) { $arr[] = EntityToArray::toArray($e, $mode, $deep + 1); } else { throw new EntityToArrayNoModeException(array($entity, false, true)); } } } $result[$name] = $arr; } } } } } return $result; }
/** * Nastavi $this->cache['fk'] * @param array of entityname * @param IRepositoryContainer */ private final function loadFk(array $entityNames, IRepositoryContainer $model) { $result = array(); if ($this->foreignKeyFormat('test') !== 'test') { foreach ($entityNames as $entityName) { foreach (MetaData::getEntityRules($entityName, $model) as $name => $rule) { if ($rule['relationship'] !== MetaData::ManyToOne and $rule['relationship'] !== MetaData::OneToOne) { continue; } $fk = $this->foreignKeyFormat($this->storageFormat($name)); $result['storage'][$fk] = $name; $result['entity'][$name] = $fk; } } } $this->cache['fk'] = $result; }