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->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;

View File

@ -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);
}

View File

@ -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

View File

@ -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<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?: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<string, array{mapper:string, external:string}>
* @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<string, array{mapper:string, external:string}>
* @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<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @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);
}
}

View File

@ -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<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?: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';
}

View File

@ -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<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?: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<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @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';
}

View File

@ -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');

View File

@ -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/';

View File

@ -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());
}
}

View File

@ -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());
}
}