- WIP on database storage filling
This commit is contained in:
1 parent
2fd9331c15
commit
7117021750
6 files changed
+208
-8
No files matched your search
@@ -27,7 +27,7 @@ class Storage extends Console {
|
||||
$migrations = $this->container->has(Lib\DatabaseMigrations::class) ? $this->container->get(Lib\DatabaseMigrations::class) : false;
|
||||
$migrations->getEntities();
|
||||
|
||||
$context = (new FormHandler($request, new Form\Storage\Database\Migrate($migrations)))->context;
|
||||
$context = (new FormHandler($request, new Form\Storage\Database\Migrate($migrations), $this->pushContext(new Lib\FormContext($request, "database.migrate"))))->context;
|
||||
|
||||
return $this->renderView("lean-console/page/storage/database", get_defined_vars());
|
||||
}
|
||||
@@ -46,6 +46,16 @@ class Storage extends Console {
|
||||
return $this->renderView("lean-console/page/storage/database_import", get_defined_vars());
|
||||
}
|
||||
|
||||
#[Route(route: "/storage/database/insert", name: "lean.console:storage.database_insert")]
|
||||
public function insert(ServerRequestInterface $request, array $arguments) : ResponseInterface
|
||||
{
|
||||
$class = $request->getQueryParams()['source'] ?? $request->getParsedBody()['source'];
|
||||
|
||||
$context = (new FormHandler($request, new Form\Storage\Database\Insert(new $class())))->context;
|
||||
|
||||
return $this->renderView("lean-console/page/storage/database_insert", get_defined_vars());
|
||||
}
|
||||
|
||||
#[Route(route: "/storage/session", name: "lean.console:storage.session")] # migrated from: "/storage/session", "name" => "lean.console:storage.session"
|
||||
public function session(ServerRequestInterface $request, array $arguments) : ResponseInterface
|
||||
{
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Lean\Console\Form\Storage\Database;
|
||||
|
||||
use Picea\Ui\Method\{ FormInterface, FormContext, FormContextInterface };
|
||||
|
||||
use \Lean\Console\{ Lib, Entity };
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Ulmus\{Attribute\Property\Field, Entity\EntityInterface, Entity\Field\Datetime, EntityCollection};
|
||||
|
||||
class Insert implements FormInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected EntityInterface $entity
|
||||
) { }
|
||||
|
||||
public function initialize(FormContextInterface $context) : void
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function validate(FormContextInterface $context) : bool
|
||||
{
|
||||
|
||||
return $context->valid();
|
||||
}
|
||||
|
||||
public function execute(FormContextInterface $context) : void
|
||||
{
|
||||
foreach ($this->entity::resolveEntity()->fieldList() as $field) {
|
||||
$type = $field->getTypes()[0];
|
||||
$value = $context->{$field->name};
|
||||
|
||||
if (! $field->getAttribute(Field::class)->object->readonly) {
|
||||
if ($type->isEnum() && $value) {
|
||||
$this->entity->{$field->name} = $type->type::from($value);
|
||||
}
|
||||
else {
|
||||
$this->entity->{$field->name} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Temp patch while fixing default value in sqlite
|
||||
if (property_exists($this->entity, 'createdAt')) {
|
||||
$this->entity->createdAt = new Datetime();
|
||||
}
|
||||
|
||||
$this->entity::repository()->save($this->entity);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user