private function replaceEntitiesForItsPrimaryKeyValues(array $entities) { foreach ($entities as &$entity) { if ($entity instanceof LeanMapper\Entity) { $entityTable = $this->mapper->getTable(get_class($entity)); // FIXME: Column name could be specified in the entity instead of mapper provided by 'getEntityField' function. $idField = $this->mapper->getEntityField($entityTable, $this->mapper->getPrimaryKey($entityTable)); $entity = $entity->{$idField}; } } return $entities; }
/** * @param $table * @return Property * @throws \Exception */ private function getRelationshipColumnProperty($table) { $class = $this->mapper->getEntityClass($table); if (!class_exists($class)) { throw new \Exception(); } /** @var Entity $entity */ $entity = new $class(); $primaryKey = $this->mapper->getPrimaryKey($table); $primryKeyField = $this->mapper->getEntityField($table, $primaryKey); return $entity->getReflection($this->mapper)->getEntityProperty($primryKeyField); }
/** * @param Entity $entity * @return mixed */ private function getIdValue(Entity $entity) { $table = $this->getTable(); do { $primaryKey = $this->mapper->getPrimaryKey($table); $idField = $this->mapper->getEntityField($table, $primaryKey); $value = $entity->{$idField}; if (!$value instanceof Entity) { return $value; } $entity = $value; $table = $this->mapper->getTable(get_class($entity)); } while (true); }
/** * Persists changes in M:N relationships * * @param Entity $entity */ protected function persistHasManyChanges(Entity $entity) { $primaryKey = $this->mapper->getPrimaryKey($this->getTable()); $idField = $this->mapper->getEntityField($this->getTable(), $primaryKey); foreach ($entity->getHasManyRowDifferences() as $key => $difference) { list($columnReferencingSourceTable, $relationshipTable, $columnReferencingTargetTable) = explode(':', $key); $multiInsert = array(); foreach ($difference as $value => $count) { if ($count > 0) { for ($i = 0; $i < $count; $i++) { $multiInsert[] = array($columnReferencingSourceTable => $entity->{$idField}, $columnReferencingTargetTable => $value); } } else { $this->connection->query('DELETE FROM %n WHERE %n = ? AND %n = ? %lmt', $relationshipTable, $columnReferencingSourceTable, $entity->{$idField}, $columnReferencingTargetTable, $value, -$count); } } if (!empty($multiInsert)) { $this->connection->query('INSERT INTO %n %ex', $relationshipTable, $multiInsert); } } }
/** * @param Entity|null $entity * @param Property|string $property micro-optimalization * @throws InvalidMethodCallException */ protected function assignEntityToProperty(Entity $entity = null, $property) { if ($entity !== null) { $this->useMapper($entity->mapper); $this->setEntityFactory($entity->entityFactory); $table = $this->mapper->getTable(get_class($entity)); $idProperty = $this->mapper->getEntityField($table, $this->mapper->getPrimaryKey($table)); } if (is_string($property)) { $property = $this->getCurrentReflection()->getEntityProperty($property); } $relationship = $property->getRelationship(); if (!$relationship instanceof Relationship\HasOne) { throw new InvalidMethodCallException("Cannot assign value to property '{$property->getName()}' in entity " . get_called_class() . '. Only properties with m:hasOne relationship can be set via magic __set.'); } $column = $relationship->getColumnReferencingTargetTable(); if ($entity !== null) { $this->row->setReferencedRow($entity->row, $column); $this->row->{$column} = $entity->{$idProperty}; } else { $this->row->{$column} = null; } }