/** * Get entities by many to many association * * @return array|NULL */ protected function getManyToManyData() { $association = Metadata::getMetadata($this->entity)->associations[$this->name]; $targetMetadata = Metadata::getMetadata($association->targetEntity); $data = $this->em->connection->select("[{$targetMetadata->tableName}].*")->from($targetMetadata->tableName)->innerJoin($association->joinTable)->on("[{$association->joinTable}].[{$association->joinSourceColumn}] = " . $this->getModificator($this->entity, $association->sourceColumn), $this->associationKey)->and("[{$association->joinTable}].[{$association->joinTargetColumn}] = [" . $targetMetadata->tableName . "].[{$association->targetColumn}]")->execute(); $entities = $this->em->getIdentityMap($association->targetEntity)->map($data->fetchAssoc($targetMetadata->primaryKey)); if (!empty($entities)) { $this->em->associationsMap->map($this->entity, $this->name, $this->associationKey, array_keys($entities)); } return $entities; }
/** * Commit * * @throws InvalidStateException */ public function commit() { try { $this->em->connection->begin(); if (count($this->deleteEntities) >= 1) { foreach ($this->deleteEntities as $entity) { $this->em->getPersister(get_class($entity))->delete($entity); $this->em->getIdentityMap(get_class($entity))->detach($entity); } } if (count($this->updateEntities) >= 1) { foreach ($this->updateEntities as $entity) { $this->persistAssociations($entity); $this->em->getPersister(get_class($entity))->update($entity); $this->em->getIdentityMap(get_class($entity))->remap($entity); } } if (count($this->insertEntities) >= 1) { foreach ($this->insertEntities as $entity) { $this->persistAssociations($entity); $persister = $this->em->getPersister(get_class($entity)); $persister->insert($entity); $metadata = Metadata::getMetadata(get_class($entity)); if ($metadata->primaryKeyAutoincrement) { $metadata->setPrimaryKeyValue($entity, $persister->lastPrimaryKey()); } $this->em->getIdentityMap(get_class($entity))->store($entity); } } $this->em->connection->commit(); $this->insertEntities = $this->updateEntities = $this->deleteEntities = array(); $this->count = 0; } catch (\DibiDriverException $e) { $this->em->connection->rollback(); throw new \InvalidStateException("When saving changes has error occurred. [" . $e->getMessage() . "] [" . \dibi::$sql . "]", NULL, $e); } }
/** * Get values * * @param mixed $entity * @return array */ protected function getValues(&$entity) { return $this->em->getIdentityMap($this->entity)->getSavedValues($entity); }