public function getKeys(IEntity $entity, $relationName) { $relOpt = $entity->relations[$relationName]; $relType = $relOpt['type']; switch ($relType) { case IEntity::BELONGS_TO: $key = $this->toDb($entity->getEntityType()) . 'Id'; if (isset($relOpt['key'])) { $key = $relOpt['key']; } $foreignKey = 'id'; if (isset($relOpt['foreignKey'])) { $foreignKey = $relOpt['foreignKey']; } return array('key' => $key, 'foreignKey' => $foreignKey); case IEntity::HAS_MANY: case IEntity::HAS_ONE: $key = 'id'; if (isset($relOpt['key'])) { $key = $relOpt['key']; } $foreignKey = $this->toDb($entity->getEntityType()) . 'Id'; if (isset($relOpt['foreignKey'])) { $foreignKey = $relOpt['foreignKey']; } return array('key' => $key, 'foreignKey' => $foreignKey); case IEntity::HAS_CHILDREN: $key = 'id'; if (isset($relOpt['key'])) { $key = $relOpt['key']; } $foreignKey = 'parentId'; if (isset($relOpt['foreignKey'])) { $foreignKey = $relOpt['foreignKey']; } $foreignType = 'parentType'; if (isset($relOpt['foreignType'])) { $foreignType = $relOpt['foreignType']; } return array('key' => $key, 'foreignKey' => $foreignKey, 'foreignType' => $foreignType); case IEntity::MANY_MANY: $key = 'id'; if (isset($relOpt['key'])) { $key = $relOpt['key']; } $foreignKey = 'id'; if (isset($relOpt['foreignKey'])) { $foreignKey = $relOpt['foreignKey']; } $nearKey = $this->toDb($entity->getEntityType()) . 'Id'; $distantKey = $this->toDb($relOpt['entity']) . 'Id'; if (isset($relOpt['midKeys']) && is_array($relOpt['midKeys'])) { $nearKey = $relOpt['midKeys'][0]; $distantKey = $relOpt['midKeys'][1]; } return array('key' => $key, 'foreignKey' => $foreignKey, 'nearKey' => $nearKey, 'distantKey' => $distantKey); } }
protected function fromRow(IEntity $entity, $data) { $entity->set($data); return $entity; }
protected function getAlias(IEntity $entity, $key) { if (!isset($this->aliasesCache[$entity->getEntityName()])) { $this->aliasesCache[$entity->getEntityName()] = $this->getTableAliases($entity); } if (isset($this->aliasesCache[$entity->getEntityName()][$key])) { return $this->aliasesCache[$entity->getEntityName()][$key]; } else { return false; } }