public function create($data) { if ($this->storagePrimaryKey === NULL) { $this->storageReflection = $this->repository->getMapper()->getStorageReflection(); $this->storagePrimaryKey = (array) $this->storageReflection->getStoragePrimaryKey(); } $id = []; foreach ($this->storagePrimaryKey as $key) { if (!isset($data[$key])) { throw new InvalidArgumentException("Data returned from storage does not contain primary value(s) for '{$key}' key."); } $id[] = $data[$key]; } $id = implode(',', $id); if (isset($this->entities[$id]) && $this->entities[$id]) { return $this->entities[$id]; } $data = $this->storageReflection->convertStorageToEntity($data); $entityClass = $this->repository->getEntityClassName($data); if (!isset($this->entityReflections[$entityClass])) { $this->entityReflections[$entityClass] = ClassType::from($entityClass); $this->entityMetadata[$entityClass] = MetadataStorage::get($entityClass); } /** @var $entity IEntity */ $entity = $this->entities[$id] = $this->entityReflections[$entityClass]->newInstanceWithoutConstructor(); $entity->fireEvent('onLoad', [$this->repository, $this->entityMetadata[$entityClass], $data]); return $entity; }
protected function fetch(SqlBuilder $builder, $hasJoin, array $values) { $primaryKey = $this->targetRepository->getMapper()->getStorageReflection()->getStoragePrimaryKey()[0]; $builder->addWhere($primaryKey, $values); $builder->addSelect(($hasJoin ? 'DISTINCT ' : '') . $builder->getTableName() . '.*'); $result = $this->context->queryArgs($builder->buildSelectQuery(), $builder->getParameters()); $entities = []; while ($data = $result->fetch()) { $entity = $this->targetRepository->hydrateEntity((array) $data); $entities[$entity->id] = $entity; } return new EntityContainer($entities); }
public function create($data) { if ($this->storagePrimaryKey === null) { $this->storageReflection = $this->repository->getMapper()->getStorageReflection(); $this->storagePrimaryKey = (array) $this->storageReflection->getStoragePrimaryKey(); } $entity = $this->createEntity($data); $id = implode(',', (array) $entity->getPersistedId()); if (isset($this->entities[$id])) { $this->repository->detach($entity); return $this->entities[$id] ?: null; } return $this->entities[$id] = $entity; // = intentionally }
protected function getParser() { if (!$this->parser) { $this->parser = new ConditionParser($this->repository->getModel(), $this->repository->getMapper()); } return $this->parser; }
protected function getHelper() { if ($this->helper === NULL) { $this->helper = new ArrayCollectionHelper($this->repository->getModel(), $this->repository->getMapper()); } return $this->helper; }
protected function getParser() { if ($this->parser === NULL) { $this->parser = new QueryBuilderHelper($this->repository->getModel(), $this->repository->getMapper()); } return $this->parser; }
private function fetchCounts(QueryBuilder $builder, array $values) { $sourceTable = $builder->getFromAlias(); $targetTable = QueryBuilderHelper::getAlias($this->joinTable); $builder = clone $builder; $builder->leftJoin($sourceTable, '%table', $targetTable, '%column = %column', $this->joinTable, "{$targetTable}.{$this->primaryKeyTo}", "{$sourceTable}." . $this->targetRepository->getMapper()->getStorageReflection()->getStoragePrimaryKey()[0]); $builder->addSelect('%column', "{$targetTable}.{$this->primaryKeyFrom}"); $builder->orderBy(NULL); if ($builder->hasLimitOffsetClause()) { $sqls = []; $args = []; foreach ($values as $value) { $build = clone $builder; $build->andWhere("%column = %any", $this->primaryKeyFrom, $value); $sqls[] = "SELECT %any AS %column, COUNT(*) AS [count] FROM (" . $build->getQuerySql() . ') [temp]'; $args[] = $value; $args[] = $this->primaryKeyFrom; $args = array_merge($args, $build->getQueryParameters()); } $sql = '(' . implode(') UNION ALL (', $sqls) . ')'; $result = $this->connection->queryArgs($sql, $args); } else { $builder->addSelect('COUNT(%column) as count', $this->primaryKeyTo); $builder->andWhere('%column IN %any', $this->primaryKeyFrom, $values); $builder->groupBy('%column', $this->primaryKeyFrom); $result = $this->connection->queryArgs($builder->getQuerySql(), $builder->getQueryParameters()); } $counts = []; foreach ($result as $row) { $counts[$row->{$this->primaryKeyFrom}] = $row->count; } return $counts; }
protected function fetch(QueryBuilder $builder, $hasJoin, array $values, IEntityPreloadContainer $preloadContainer = NULL) { $values = array_values(array_unique(array_filter($values, function ($value) { return $value !== NULL; }))); if (count($values) === 0) { return new EntityContainer([], $preloadContainer); } $primaryKey = $this->targetRepository->getMapper()->getStorageReflection()->getStoragePrimaryKey()[0]; $builder->andWhere('%column IN %any', $primaryKey, $values); $builder->addSelect(($hasJoin ? 'DISTINCT ' : '') . '%table.*', $builder->getFromAlias()); $result = $this->connection->queryArgs($builder->getQuerySQL(), $builder->getQueryParameters()); $entities = []; while ($data = $result->fetch()) { $entity = $this->targetRepository->hydrateEntity($data->toArray()); $entities[$entity->getValue('id')] = $entity; } return new EntityContainer($entities, $preloadContainer); }
public function __construct(Nextras\Orm\Entity\IEntity $entity, Nextras\Orm\Repository\IRepository $repository) { parent::__construct(); $this->entity = $entity; $this->repository = $repository; $this->metadata = $entity->getMetadata(); $this->mapper = $repository->getMapper(); $this->model = $repository->getModel(); $this->monitor(Ytnuk\Orm\Form::class); $repository->attach($entity); }