new datamapper mostly implemented

This commit is contained in:
Dennis Eichhorn 2021-12-11 11:54:17 +01:00
parent e526749bde
commit ae138a2912
10 changed files with 49 additions and 52 deletions

View File

@ -124,13 +124,13 @@ final class Installer extends InstallerAbstract
$type = new EditorDocType(); $type = new EditorDocType();
$type->name = $data['name'] ?? ''; $type->name = $data['name'] ?? '';
$id = EditorDocTypeMapper::create($type); $id = EditorDocTypeMapper::create()->execute($type);
foreach ($data['l11n'] as $l11n) { foreach ($data['l11n'] as $l11n) {
$l11n = new EditorDocTypeL11n($l11n['title'], $l11n['lang']); $l11n = new EditorDocTypeL11n($l11n['title'], $l11n['lang']);
$l11n->type = $id; $l11n->type = $id;
EditorDocTypeL11nMapper::create($l11n); EditorDocTypeL11nMapper::create()->execute($l11n);
} }
return $type; return $type;

View File

@ -161,7 +161,7 @@ final class ApiController extends Controller
public function apiEditorUpdate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void public function apiEditorUpdate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{ {
/** @var \Modules\Editor\Models\EditorDoc $old */ /** @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); $new = $this->updateEditorFromRequest($request);
$this->updateModel($request->header->account, $old, $new, EditorDocMapper::class, 'doc', $request->getOrigin()); $this->updateModel($request->header->account, $old, $new, EditorDocMapper::class, 'doc', $request->getOrigin());
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Document', 'Document successfully updated', $new); $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 private function updateEditorFromRequest(RequestAbstract $request) : EditorDoc
{ {
/** @var \Modules\Editor\Models\EditorDoc $doc */ /** @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->title = (string) ($request->getData('title') ?? $doc->title);
$doc->plain = (string) ($request->getData('plain') ?? $doc->plain); $doc->plain = (string) ($request->getData('plain') ?? $doc->plain);
$doc->content = Markdown::parse((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 public function apiEditorGet(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{ {
/** @var \Modules\Editor\Models\EditorDoc $doc */ /** @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); $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 public function apiEditorDelete(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{ {
/** @var \Modules\Editor\Models\EditorDoc $doc */ /** @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->deleteModel($request->header->account, $doc, EditorDocMapper::class, 'doc', $request->getOrigin());
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Document', 'Document successfully deleted', $doc); $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Document', 'Document successfully deleted', $doc);
} }

View File

@ -102,7 +102,7 @@ final class BackendController extends Controller
$view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1005301001, $request, $response)); $view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1005301001, $request, $response));
$path = \str_replace('+', ' ', (string) ($request->getData('path') ?? '/')); $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); list($collection, $parent) = CollectionMapper::getCollectionsByPath($path);
@ -132,7 +132,7 @@ final class BackendController extends Controller
$view = new View($this->app->l11nManager, $request, $response); $view = new View($this->app->l11nManager, $request, $response);
/** @var \Modules\Editor\Models\EditorDoc $doc */ /** @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; $accountId = $request->header->account;
if ($doc->createdBy->getId() !== $accountId if ($doc->createdBy->getId() !== $accountId
@ -178,7 +178,7 @@ final class BackendController extends Controller
$view = new View($this->app->l11nManager, $request, $response); $view = new View($this->app->l11nManager, $request, $response);
/** @var \Modules\Editor\Models\EditorDoc $doc */ /** @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; $accountId = $request->header->account;
if ($doc->createdBy->getId() !== $accountId if ($doc->createdBy->getId() !== $accountId

View File

@ -17,8 +17,8 @@ namespace Modules\Editor\Models;
use Modules\Admin\Models\AccountMapper; use Modules\Admin\Models\AccountMapper;
use Modules\Media\Models\MediaMapper; use Modules\Media\Models\MediaMapper;
use Modules\Tag\Models\TagMapper; use Modules\Tag\Models\TagMapper;
use phpOMS\DataStorage\Database\DataMapperAbstract; use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
use phpOMS\DataStorage\Database\RelationType; use phpOMS\DataStorage\Database\Mapper\ReadMapper;
/** /**
* Editor doc mapper class. * Editor doc mapper class.
@ -28,7 +28,7 @@ use phpOMS\DataStorage\Database\RelationType;
* @link https://orange-management.org * @link https://orange-management.org
* @since 1.0.0 * @since 1.0.0
*/ */
final class EditorDocMapper extends DataMapperAbstract final class EditorDocMapper extends DataMapperFactory
{ {
/** /**
* Columns. * Columns.
@ -36,7 +36,7 @@ final class EditorDocMapper extends DataMapperAbstract
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}> * @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
* @since 1.0.0 * @since 1.0.0
*/ */
protected static array $columns = [ public const COLUMNS = [
'editor_doc_id' => ['name' => 'editor_doc_id', 'type' => 'int', 'internal' => 'id'], '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_created_by' => ['name' => 'editor_doc_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true],
'editor_doc_title' => ['name' => 'editor_doc_title', 'type' => 'string', 'internal' => 'title'], 'editor_doc_title' => ['name' => 'editor_doc_title', 'type' => 'string', 'internal' => 'title'],
@ -54,7 +54,7 @@ final class EditorDocMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, external:string}> * @var array<string, array{mapper:string, external:string}>
* @since 1.0.0 * @since 1.0.0
*/ */
protected static array $belongsTo = [ public const BELONGS_TO = [
'createdBy' => [ 'createdBy' => [
'mapper' => AccountMapper::class, 'mapper' => AccountMapper::class,
'external' => 'editor_doc_created_by', 'external' => 'editor_doc_created_by',
@ -67,7 +67,7 @@ final class EditorDocMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, external:string}> * @var array<string, array{mapper:string, external:string}>
* @since 1.0.0 * @since 1.0.0
*/ */
protected static array $ownsOne = [ public const OWNS_ONE = [
'type' => [ 'type' => [
'mapper' => EditorDocTypeMapper::class, 'mapper' => EditorDocTypeMapper::class,
'external' => 'editor_doc_type', 'external' => 'editor_doc_type',
@ -80,7 +80,7 @@ final class EditorDocMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}> * @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @since 1.0.0 * @since 1.0.0
*/ */
protected static array $hasMany = [ public const HAS_MANY = [
'tags' => [ 'tags' => [
'mapper' => TagMapper::class, 'mapper' => TagMapper::class,
'table' => 'editor_doc_tag', 'table' => 'editor_doc_tag',
@ -101,7 +101,7 @@ final class EditorDocMapper extends DataMapperAbstract
* @var string * @var string
* @since 1.0.0 * @since 1.0.0
*/ */
protected static string $table = 'editor_doc'; public const TABLE = 'editor_doc';
/** /**
* Primary field name. * Primary field name.
@ -109,7 +109,7 @@ final class EditorDocMapper extends DataMapperAbstract
* @var string * @var string
* @since 1.0.0 * @since 1.0.0
*/ */
protected static string $primaryField = 'editor_doc_id'; public const PRIMARYFIELD ='editor_doc_id';
/** /**
* Created at. * Created at.
@ -117,7 +117,7 @@ final class EditorDocMapper extends DataMapperAbstract
* @var string * @var string
* @since 1.0.0 * @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. * Get editor doc based on virtual path.
@ -125,20 +125,17 @@ final class EditorDocMapper extends DataMapperAbstract
* @param string $virtualPath Virtual path * @param string $virtualPath Virtual path
* @param int $account Account id * @param int $account Account id
* *
* @return array * @return ReadMapper
* *
* @since 1.0.0 * @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; return self::getAll()
$query = self::getQuery(depth: $depth); ->with('createdBy')
$query->where(self::$table . '_d' . $depth . '.editor_doc_virtual', '=', $virtualPath); ->with('tags')
->with('tags/title')
if (!empty($account)) { ->where('virtualPath', $virtualPath)
$query->where(self::$table . '_d' . $depth . '.editor_doc_created_by', '=', $account); ->where('createdBy', $account);
}
return self::getAllByQuery($query, RelationType::ALL, $depth);
} }
} }

View File

@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Modules\Editor\Models; namespace Modules\Editor\Models;
use phpOMS\DataStorage\Database\DataMapperAbstract; use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
/** /**
* Editor type l11n mapper class. * Editor type l11n mapper class.
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
* @link https://orange-management.org * @link https://orange-management.org
* @since 1.0.0 * @since 1.0.0
*/ */
final class EditorDocTypeL11nMapper extends DataMapperAbstract final class EditorDocTypeL11nMapper extends DataMapperFactory
{ {
/** /**
* Columns. * Columns.
@ -32,7 +32,7 @@ final class EditorDocTypeL11nMapper extends DataMapperAbstract
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}> * @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
* @since 1.0.0 * @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_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_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'], '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 * @var string
* @since 1.0.0 * @since 1.0.0
*/ */
protected static string $table = 'editor_doc_type_l11n'; public const TABLE = 'editor_doc_type_l11n';
/** /**
* Primary field name. * Primary field name.
@ -53,5 +53,5 @@ final class EditorDocTypeL11nMapper extends DataMapperAbstract
* @var string * @var string
* @since 1.0.0 * @since 1.0.0
*/ */
protected static string $primaryField = 'editor_doc_type_l11n_id'; public const PRIMARYFIELD ='editor_doc_type_l11n_id';
} }

View File

@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Modules\Editor\Models; namespace Modules\Editor\Models;
use phpOMS\DataStorage\Database\DataMapperAbstract; use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
/** /**
* Editor type mapper class. * Editor type mapper class.
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
* @link https://orange-management.org * @link https://orange-management.org
* @since 1.0.0 * @since 1.0.0
*/ */
final class EditorDocTypeMapper extends DataMapperAbstract final class EditorDocTypeMapper extends DataMapperFactory
{ {
/** /**
* Columns. * Columns.
@ -32,7 +32,7 @@ final class EditorDocTypeMapper extends DataMapperAbstract
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}> * @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
* @since 1.0.0 * @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_id' => ['name' => 'editor_doc_type_id', 'type' => 'int', 'internal' => 'id'],
'editor_doc_type_name' => ['name' => 'editor_doc_type_name', 'type' => 'string', 'internal' => 'name'], 'editor_doc_type_name' => ['name' => 'editor_doc_type_name', 'type' => 'string', 'internal' => 'name'],
]; ];
@ -43,7 +43,7 @@ final class EditorDocTypeMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}> * @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @since 1.0.0 * @since 1.0.0
*/ */
protected static array $hasMany = [ public const HAS_MANY = [
'title' => [ 'title' => [
'mapper' => EditorDocTypeL11nMapper::class, 'mapper' => EditorDocTypeL11nMapper::class,
'table' => 'editor_doc_type_l11n', 'table' => 'editor_doc_type_l11n',
@ -60,7 +60,7 @@ final class EditorDocTypeMapper extends DataMapperAbstract
* @var string * @var string
* @since 1.0.0 * @since 1.0.0
*/ */
protected static string $model = EditorDocType::class; public const MODEL = EditorDocType::class;
/** /**
* Primary table. * Primary table.
@ -68,7 +68,7 @@ final class EditorDocTypeMapper extends DataMapperAbstract
* @var string * @var string
* @since 1.0.0 * @since 1.0.0
*/ */
protected static string $table = 'editor_doc_type'; public const TABLE = 'editor_doc_type';
/** /**
* Primary field name. * Primary field name.
@ -76,5 +76,5 @@ final class EditorDocTypeMapper extends DataMapperAbstract
* @var string * @var string
* @since 1.0.0 * @since 1.0.0
*/ */
protected static string $primaryField = 'editor_doc_type_id'; public const PRIMARYFIELD ='editor_doc_type_id';
} }

View File

@ -18,7 +18,7 @@ use phpOMS\Uri\UriFactory;
* @var \phpOMS\Views\View $this * @var \phpOMS\Views\View $this
* @var \Modules\Editor\Models\EditorDoc[] $docs * @var \Modules\Editor\Models\EditorDoc[] $docs
*/ */
$docs = $this->getData('docs'); $docs = $this->getData('docs') ?? [];
/** @var \Modules\Media\Models\Collection[] */ /** @var \Modules\Media\Models\Collection[] */
$collections = $this->getData('collections'); $collections = $this->getData('collections');

View File

@ -12,7 +12,7 @@ require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/Autoloader.php'; require_once __DIR__ . '/Autoloader.php';
use phpOMS\DataStorage\Database\DatabasePool; use phpOMS\DataStorage\Database\DatabasePool;
use phpOMS\DataStorage\Database\DataMapperAbstract; use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
use phpOMS\DataStorage\Session\HttpSession; use phpOMS\DataStorage\Session\HttpSession;
$CONFIG = [ $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('insert', $CONFIG['db']['core']['masters']['insert']);
$GLOBALS['dbpool']->create('schema', $CONFIG['db']['core']['masters']['schema']); $GLOBALS['dbpool']->create('schema', $CONFIG['db']['core']['masters']['schema']);
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get()); DataMapperFactory::db($GLOBALS['dbpool']->get());
$GLOBALS['frameworkpath'] = '/phpOMS/'; $GLOBALS['frameworkpath'] = '/phpOMS/';

View File

@ -225,7 +225,7 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase
$this->module->apiEditorUpdate($request, $response); $this->module->apiEditorUpdate($request, $response);
self::assertEquals('Changed Title', $response->get('')['response']->title); 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()); self::assertEquals(1, $response->get('')['response']->getId());
} }
@ -256,7 +256,7 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase
$doc->content = 'TestContent'; $doc->content = 'TestContent';
$doc->createdBy = new NullAccount(1); $doc->createdBy = new NullAccount(1);
$docId = EditorDocMapper::create($doc); $docId = EditorDocMapper::create()->execute($doc);
$response = new HttpResponse(); $response = new HttpResponse();
$request = new HttpRequest(new HttpUri('')); $request = new HttpRequest(new HttpUri(''));
@ -267,6 +267,6 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase
$this->module->apiEditorDelete($request, $response); $this->module->apiEditorDelete($request, $response);
self::assertEquals($docId, $response->get('')['response']->getId()); self::assertEquals($docId, $response->get('')['response']->getId());
self::assertInstanceOf(NullEditorDoc::class, EditorDocMapper::get($docId)); self::assertInstanceOf(NullEditorDoc::class, EditorDocMapper::get()->where('id', $docId)->execute());
} }
} }

View File

@ -36,18 +36,18 @@ final class EditorDocMapperTest extends \PHPUnit\Framework\TestCase
$doc->content = 'Content'; $doc->content = 'Content';
$doc->setVirtualPath('/some/test/path'); $doc->setVirtualPath('/some/test/path');
$id = EditorDocMapper::create($doc); $id = EditorDocMapper::create()->execute($doc);
self::assertGreaterThan(0, $doc->getId()); self::assertGreaterThan(0, $doc->getId());
self::assertEquals($id, $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->createdAt->format('Y-m-d'), $docR->createdAt->format('Y-m-d'));
self::assertEquals($doc->createdBy->getId(), $docR->createdBy->getId()); self::assertEquals($doc->createdBy->getId(), $docR->createdBy->getId());
self::assertEquals($doc->content, $docR->content); self::assertEquals($doc->content, $docR->content);
self::assertEquals($doc->title, $docR->title); self::assertEquals($doc->title, $docR->title);
self::assertEquals($doc->getVirtualPath(), $docR->getVirtualPath()); self::assertEquals($doc->getVirtualPath(), $docR->getVirtualPath());
$docR2 = EditorDocMapper::getByVirtualPath('/some/test/path', 1); $docR2 = EditorDocMapper::getByVirtualPath('/some/test/path', 1)->execute();
self::assertEquals($docR, \reset($docR2)); self::assertEquals($docR->getId(), \reset($docR2)->getId());
} }
} }