/** * @return \Hynage\ORM\EntityCollection|\Hynage\ORM\Entity */ public function load() { if ($this->data) { return $this->data; } $constraints = array($this->foreignFieldName => $this->entity->getValue($this->localFieldName)); $type = $this->type; switch ($type) { case self::TYPE_ENTITY: $this->data = $this->em->findEntityBy($this->className, $constraints); break; case self::TYPE_COLLECTION: $this->data = $this->em->findEntitiesBy($this->className, $constraints); break; default: throw new \InvalidArgumentException("Invalid proxy type '{$type}'."); } return $this->data; }
/** * @param \Hynage\ORM\Entity $entity * @return DatabasePersistence */ public function store(Entity $entity) { $db = $this->getConnection(); $tableName = $entity::getEntityType(); $params = array(); $autoIncrementField = null; foreach ($entity::getFieldDefinitions() as $field) { if ($field->isAutoIncrement()) { $autoIncrementField = $field; continue; } $value = $entity->getValue($field); // Skip non-NOT-NULL-fields (=NULL-fields) with NULL value if (null === $value && !$field->isNotNull()) { continue; } // Set default value if (null === $value) { $value = $field->getDefaultValue(); } // Get a proper date string if ($value instanceof \DateTime) { $value = $value->format('Y-m-d H:i:s'); } $params[] = (object) array('value' => $value, 'key' => $field->getName(), 'placeholder' => $field->getPlaceholder()); } // Update if ($entity->isPersistent()) { $placeholders = $values = array(); foreach ($params as $param) { $placeholders[] = sprintf('`%s` = %s', $param->key, $param->placeholder); $values = array_merge($values, (array) $param->value); } $sql = sprintf("UPDATE `%s` SET %s WHERE %s LIMIT 1", $tableName, join(', ', $placeholders), $this->buildWhereForPrimaryKeyFields($entity)); foreach ($entity::getPrimaryKeyFields() as $pk) { $values[] = $entity->getValue($pk); } $stmt = $db->prepare($sql); $stmt->execute($values); } else { $keys = $placeholders = $values = array(); foreach ($params as $param) { $keys[] = "`{$param->key}`"; $placeholders[] = $param->placeholder; $values = array_merge($values, (array) $param->value); } $sql = sprintf("INSERT INTO `%s` (%s) VALUES (%s)", $tableName, join(', ', $keys), join(', ', $placeholders)); $stmt = $db->prepare($sql); $stmt->execute($values); if (null !== $autoIncrementField) { $entity->setValue($autoIncrementField, $db->getAdapter()->lastInsertId()); } $entity->setIsPersistent(true); } return $this; }
/** * @param \Hynage\ORM\Entity $entity * @return \Hynage\ORM\EntityManager */ private function addProxies(Entity $entity) { $reflectionClass = new ReflectionClass($entity::getClassNameOfEntityDefinition()); foreach ($reflectionClass->getProperties(\ReflectionMethod::IS_PROTECTED) as $property) { $definition = $property->getAnnotation('HynageRelation'); if (!is_array($definition)) { continue; } $proxy = new Proxy($this, $entity, $definition['class'], $definition['local'], $definition['foreign'], $definition['type']); $entity->setProxy($property->name, $proxy); } return $this; }