From a319f3a419790925bed539ba141038c72a83e70f Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 10 Jan 2022 20:29:04 +0100 Subject: Initial commit --- src/DB.php | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/DB.php (limited to 'src/DB.php') diff --git a/src/DB.php b/src/DB.php new file mode 100644 index 0000000..c04d189 --- /dev/null +++ b/src/DB.php @@ -0,0 +1,55 @@ +setMetadataDriverImpl($config->newDefaultAnnotationDriver([__DIR__ . '/Model'], true)); + #$config->setMetadataDriverImpl(new AttributeDriver([__DIR__ . '/Model'])); + + $conn = array( + 'driver' => 'pdo_sqlite', + 'path' => dirname(__DIR__) . '/db.sqlite', + ); + + self::$entityManager = EntityManager::create($conn, $config); + } + + /** + * @param object $entity + * @param array $criteria + */ + public static function save(object $entity, array $criteria = []) + { + $repository = self::$entityManager->getRepository(get_class($entity)); + $exists = $repository->findOneBy($criteria); + + if (! empty($criteria) && ! is_null($exists)) { + foreach (get_object_vars($entity) as $key => $value) { + $exists->$key = $value; + } + } else { + $exists = $entity; + } + + self::$entityManager->persist($exists); + self::$entityManager->flush(); + } +} + -- cgit v1.2.3