From ae138a29124d09501e4ceefcabf8365a6d5c8ba4 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 11 Dec 2021 11:54:17 +0100 Subject: [PATCH] new datamapper mostly implemented --- Admin/Installer.php | 4 +-- Controller/ApiController.php | 8 +++--- Controller/BackendController.php | 6 ++-- Models/EditorDocMapper.php | 39 ++++++++++++-------------- Models/EditorDocTypeL11nMapper.php | 10 +++---- Models/EditorDocTypeMapper.php | 14 ++++----- Theme/Backend/editor-list.tpl.php | 2 +- tests/Bootstrap.php | 4 +-- tests/Controller/ApiControllerTest.php | 6 ++-- tests/Models/EditorDocMapperTest.php | 8 +++--- 10 files changed, 49 insertions(+), 52 deletions(-) diff --git a/Admin/Installer.php b/Admin/Installer.php index 1d4dbb6..fa029a3 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -124,13 +124,13 @@ final class Installer extends InstallerAbstract $type = new EditorDocType(); $type->name = $data['name'] ?? ''; - $id = EditorDocTypeMapper::create($type); + $id = EditorDocTypeMapper::create()->execute($type); foreach ($data['l11n'] as $l11n) { $l11n = new EditorDocTypeL11n($l11n['title'], $l11n['lang']); $l11n->type = $id; - EditorDocTypeL11nMapper::create($l11n); + EditorDocTypeL11nMapper::create()->execute($l11n); } return $type; diff --git a/Controller/ApiController.php b/Controller/ApiController.php index c00e9b7..a12223c 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -161,7 +161,7 @@ final class ApiController extends Controller public function apiEditorUpdate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void { /** @var \Modules\Editor\Models\EditorDoc $old */ - $old = clone EditorDocMapper::get((int) $request->getData('id')); + $old = clone EditorDocMapper::get()->where('id', (int) $request->getData('id'))->execute(); $new = $this->updateEditorFromRequest($request); $this->updateModel($request->header->account, $old, $new, EditorDocMapper::class, 'doc', $request->getOrigin()); $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Document', 'Document successfully updated', $new); @@ -179,7 +179,7 @@ final class ApiController extends Controller private function updateEditorFromRequest(RequestAbstract $request) : EditorDoc { /** @var \Modules\Editor\Models\EditorDoc $doc */ - $doc = EditorDocMapper::get((int) $request->getData('id')); + $doc = EditorDocMapper::get()->where('id', (int) $request->getData('id'))->execute(); $doc->title = (string) ($request->getData('title') ?? $doc->title); $doc->plain = (string) ($request->getData('plain') ?? $doc->plain); $doc->content = Markdown::parse((string) ($request->getData('plain') ?? $doc->plain)); @@ -203,7 +203,7 @@ final class ApiController extends Controller public function apiEditorGet(RequestAbstract $request, ResponseAbstract $response, $data = null) : void { /** @var \Modules\Editor\Models\EditorDoc $doc */ - $doc = EditorDocMapper::get((int) $request->getData('id')); + $doc = EditorDocMapper::get()->where('id', (int) $request->getData('id'))->execute(); $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Document', 'Document successfully returned', $doc); } @@ -223,7 +223,7 @@ final class ApiController extends Controller public function apiEditorDelete(RequestAbstract $request, ResponseAbstract $response, $data = null) : void { /** @var \Modules\Editor\Models\EditorDoc $doc */ - $doc = EditorDocMapper::get((int) $request->getData('id')); + $doc = EditorDocMapper::get()->where('id', (int) $request->getData('id'))->execute(); $this->deleteModel($request->header->account, $doc, EditorDocMapper::class, 'doc', $request->getOrigin()); $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Document', 'Document successfully deleted', $doc); } diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 09ae38c..7fa1f1a 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -102,7 +102,7 @@ final class BackendController extends Controller $view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1005301001, $request, $response)); $path = \str_replace('+', ' ', (string) ($request->getData('path') ?? '/')); - $docs = EditorDocMapper::with('language', $response->getLanguage())::getByVirtualPath($path, $request->header->account); + $docs = EditorDocMapper::getByVirtualPath($path, $request->header->account)->where('tags/title/language', $response->getLanguage())->execute(); list($collection, $parent) = CollectionMapper::getCollectionsByPath($path); @@ -132,7 +132,7 @@ final class BackendController extends Controller $view = new View($this->app->l11nManager, $request, $response); /** @var \Modules\Editor\Models\EditorDoc $doc */ - $doc = EditorDocMapper::with('language', $response->getLanguage())::get((int) $request->getData('id')); + $doc = EditorDocMapper::get()->with('tags')->with('tags/title')->where('id', (int) $request->getData('id'))->where('tags/title/language', $response->getLanguage())->execute(); $accountId = $request->header->account; if ($doc->createdBy->getId() !== $accountId @@ -178,7 +178,7 @@ final class BackendController extends Controller $view = new View($this->app->l11nManager, $request, $response); /** @var \Modules\Editor\Models\EditorDoc $doc */ - $doc = EditorDocMapper::get((int) $request->getData('id')); + $doc = EditorDocMapper::get()->where('id', (int) $request->getData('id'))->execute(); $accountId = $request->header->account; if ($doc->createdBy->getId() !== $accountId diff --git a/Models/EditorDocMapper.php b/Models/EditorDocMapper.php index 5195f29..dfbf1cd 100755 --- a/Models/EditorDocMapper.php +++ b/Models/EditorDocMapper.php @@ -17,8 +17,8 @@ namespace Modules\Editor\Models; use Modules\Admin\Models\AccountMapper; use Modules\Media\Models\MediaMapper; use Modules\Tag\Models\TagMapper; -use phpOMS\DataStorage\Database\DataMapperAbstract; -use phpOMS\DataStorage\Database\RelationType; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; +use phpOMS\DataStorage\Database\Mapper\ReadMapper; /** * Editor doc mapper class. @@ -28,7 +28,7 @@ use phpOMS\DataStorage\Database\RelationType; * @link https://orange-management.org * @since 1.0.0 */ -final class EditorDocMapper extends DataMapperAbstract +final class EditorDocMapper extends DataMapperFactory { /** * Columns. @@ -36,7 +36,7 @@ final class EditorDocMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $columns = [ + public const COLUMNS = [ 'editor_doc_id' => ['name' => 'editor_doc_id', 'type' => 'int', 'internal' => 'id'], 'editor_doc_created_by' => ['name' => 'editor_doc_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], 'editor_doc_title' => ['name' => 'editor_doc_title', 'type' => 'string', 'internal' => 'title'], @@ -54,7 +54,7 @@ final class EditorDocMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $belongsTo = [ + public const BELONGS_TO = [ 'createdBy' => [ 'mapper' => AccountMapper::class, 'external' => 'editor_doc_created_by', @@ -67,7 +67,7 @@ final class EditorDocMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $ownsOne = [ + public const OWNS_ONE = [ 'type' => [ 'mapper' => EditorDocTypeMapper::class, 'external' => 'editor_doc_type', @@ -80,7 +80,7 @@ final class EditorDocMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $hasMany = [ + public const HAS_MANY = [ 'tags' => [ 'mapper' => TagMapper::class, 'table' => 'editor_doc_tag', @@ -101,7 +101,7 @@ final class EditorDocMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $table = 'editor_doc'; + public const TABLE = 'editor_doc'; /** * Primary field name. @@ -109,7 +109,7 @@ final class EditorDocMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $primaryField = 'editor_doc_id'; + public const PRIMARYFIELD ='editor_doc_id'; /** * Created at. @@ -117,7 +117,7 @@ final class EditorDocMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $createdAt = 'editor_doc_created_at'; + public const CREATED_AT = 'editor_doc_created_at'; /** * Get editor doc based on virtual path. @@ -125,20 +125,17 @@ final class EditorDocMapper extends DataMapperAbstract * @param string $virtualPath Virtual path * @param int $account Account id * - * @return array + * @return ReadMapper * * @since 1.0.0 */ - public static function getByVirtualPath(string $virtualPath = '/', int $account = 0) : array + public static function getByVirtualPath(string $virtualPath = '/', int $account = 0) : ReadMapper { - $depth = 3; - $query = self::getQuery(depth: $depth); - $query->where(self::$table . '_d' . $depth . '.editor_doc_virtual', '=', $virtualPath); - - if (!empty($account)) { - $query->where(self::$table . '_d' . $depth . '.editor_doc_created_by', '=', $account); - } - - return self::getAllByQuery($query, RelationType::ALL, $depth); + return self::getAll() + ->with('createdBy') + ->with('tags') + ->with('tags/title') + ->where('virtualPath', $virtualPath) + ->where('createdBy', $account); } } diff --git a/Models/EditorDocTypeL11nMapper.php b/Models/EditorDocTypeL11nMapper.php index bd37889..a01ccf1 100644 --- a/Models/EditorDocTypeL11nMapper.php +++ b/Models/EditorDocTypeL11nMapper.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace Modules\Editor\Models; -use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; /** * Editor type l11n mapper class. @@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract; * @link https://orange-management.org * @since 1.0.0 */ -final class EditorDocTypeL11nMapper extends DataMapperAbstract +final class EditorDocTypeL11nMapper extends DataMapperFactory { /** * Columns. @@ -32,7 +32,7 @@ final class EditorDocTypeL11nMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $columns = [ + public const COLUMNS = [ 'editor_doc_type_l11n_id' => ['name' => 'editor_doc_type_l11n_id', 'type' => 'int', 'internal' => 'id'], 'editor_doc_type_l11n_title' => ['name' => 'editor_doc_type_l11n_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true], 'editor_doc_type_l11n_type' => ['name' => 'editor_doc_type_l11n_type', 'type' => 'int', 'internal' => 'type'], @@ -45,7 +45,7 @@ final class EditorDocTypeL11nMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $table = 'editor_doc_type_l11n'; + public const TABLE = 'editor_doc_type_l11n'; /** * Primary field name. @@ -53,5 +53,5 @@ final class EditorDocTypeL11nMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $primaryField = 'editor_doc_type_l11n_id'; + public const PRIMARYFIELD ='editor_doc_type_l11n_id'; } diff --git a/Models/EditorDocTypeMapper.php b/Models/EditorDocTypeMapper.php index 02a7168..5903461 100644 --- a/Models/EditorDocTypeMapper.php +++ b/Models/EditorDocTypeMapper.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace Modules\Editor\Models; -use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; /** * Editor type mapper class. @@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract; * @link https://orange-management.org * @since 1.0.0 */ -final class EditorDocTypeMapper extends DataMapperAbstract +final class EditorDocTypeMapper extends DataMapperFactory { /** * Columns. @@ -32,7 +32,7 @@ final class EditorDocTypeMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $columns = [ + public const COLUMNS = [ 'editor_doc_type_id' => ['name' => 'editor_doc_type_id', 'type' => 'int', 'internal' => 'id'], 'editor_doc_type_name' => ['name' => 'editor_doc_type_name', 'type' => 'string', 'internal' => 'name'], ]; @@ -43,7 +43,7 @@ final class EditorDocTypeMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $hasMany = [ + public const HAS_MANY = [ 'title' => [ 'mapper' => EditorDocTypeL11nMapper::class, 'table' => 'editor_doc_type_l11n', @@ -60,7 +60,7 @@ final class EditorDocTypeMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $model = EditorDocType::class; + public const MODEL = EditorDocType::class; /** * Primary table. @@ -68,7 +68,7 @@ final class EditorDocTypeMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $table = 'editor_doc_type'; + public const TABLE = 'editor_doc_type'; /** * Primary field name. @@ -76,5 +76,5 @@ final class EditorDocTypeMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $primaryField = 'editor_doc_type_id'; + public const PRIMARYFIELD ='editor_doc_type_id'; } diff --git a/Theme/Backend/editor-list.tpl.php b/Theme/Backend/editor-list.tpl.php index c711683..f714a93 100755 --- a/Theme/Backend/editor-list.tpl.php +++ b/Theme/Backend/editor-list.tpl.php @@ -18,7 +18,7 @@ use phpOMS\Uri\UriFactory; * @var \phpOMS\Views\View $this * @var \Modules\Editor\Models\EditorDoc[] $docs */ -$docs = $this->getData('docs'); +$docs = $this->getData('docs') ?? []; /** @var \Modules\Media\Models\Collection[] */ $collections = $this->getData('collections'); diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index f5fc99c..252a62f 100755 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -12,7 +12,7 @@ require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/Autoloader.php'; use phpOMS\DataStorage\Database\DatabasePool; -use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; use phpOMS\DataStorage\Session\HttpSession; $CONFIG = [ @@ -329,7 +329,7 @@ $GLOBALS['dbpool']->create('delete', $CONFIG['db']['core']['masters']['delete']) $GLOBALS['dbpool']->create('insert', $CONFIG['db']['core']['masters']['insert']); $GLOBALS['dbpool']->create('schema', $CONFIG['db']['core']['masters']['schema']); -DataMapperAbstract::setConnection($GLOBALS['dbpool']->get()); +DataMapperFactory::db($GLOBALS['dbpool']->get()); $GLOBALS['frameworkpath'] = '/phpOMS/'; diff --git a/tests/Controller/ApiControllerTest.php b/tests/Controller/ApiControllerTest.php index 8e72f46..8a2fe25 100755 --- a/tests/Controller/ApiControllerTest.php +++ b/tests/Controller/ApiControllerTest.php @@ -225,7 +225,7 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase $this->module->apiEditorUpdate($request, $response); self::assertEquals('Changed Title', $response->get('')['response']->title); - self::assertEquals('Changed Title', EditorDocMapper::get(1)->title); + self::assertEquals('Changed Title', EditorDocMapper::get()->where('id', 1)->execute()->title); self::assertEquals(1, $response->get('')['response']->getId()); } @@ -256,7 +256,7 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase $doc->content = 'TestContent'; $doc->createdBy = new NullAccount(1); - $docId = EditorDocMapper::create($doc); + $docId = EditorDocMapper::create()->execute($doc); $response = new HttpResponse(); $request = new HttpRequest(new HttpUri('')); @@ -267,6 +267,6 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase $this->module->apiEditorDelete($request, $response); self::assertEquals($docId, $response->get('')['response']->getId()); - self::assertInstanceOf(NullEditorDoc::class, EditorDocMapper::get($docId)); + self::assertInstanceOf(NullEditorDoc::class, EditorDocMapper::get()->where('id', $docId)->execute()); } } diff --git a/tests/Models/EditorDocMapperTest.php b/tests/Models/EditorDocMapperTest.php index 3b2cbc4..bcb10b7 100755 --- a/tests/Models/EditorDocMapperTest.php +++ b/tests/Models/EditorDocMapperTest.php @@ -36,18 +36,18 @@ final class EditorDocMapperTest extends \PHPUnit\Framework\TestCase $doc->content = 'Content'; $doc->setVirtualPath('/some/test/path'); - $id = EditorDocMapper::create($doc); + $id = EditorDocMapper::create()->execute($doc); self::assertGreaterThan(0, $doc->getId()); self::assertEquals($id, $doc->getId()); - $docR = EditorDocMapper::get($doc->getId()); + $docR = EditorDocMapper::get()->where('id', $doc->getId())->execute(); self::assertEquals($doc->createdAt->format('Y-m-d'), $docR->createdAt->format('Y-m-d')); self::assertEquals($doc->createdBy->getId(), $docR->createdBy->getId()); self::assertEquals($doc->content, $docR->content); self::assertEquals($doc->title, $docR->title); self::assertEquals($doc->getVirtualPath(), $docR->getVirtualPath()); - $docR2 = EditorDocMapper::getByVirtualPath('/some/test/path', 1); - self::assertEquals($docR, \reset($docR2)); + $docR2 = EditorDocMapper::getByVirtualPath('/some/test/path', 1)->execute(); + self::assertEquals($docR->getId(), \reset($docR2)->getId()); } }