Bladeren bron

allow entity objects as QueryBuilder params and relax SyncCommand env check

michelphp 2 weken geleden
bovenliggende
commit
aabf2b2c8d

+ 1 - 12
src/Command/DatabaseSyncCommand.php

@@ -18,18 +18,14 @@ class DatabaseSyncCommand implements CommandInterface
 
     private EntityDirCollector $entityDirCollector;
 
-    private ?string $env;
-
     /**
      * @param PaperMigration $paperMigration
      * @param EntityDirCollector $entityDirCollector
-     * @param string|null $env
      */
-    public function __construct(PaperMigration $paperMigration, EntityDirCollector $entityDirCollector, ?string $env = null)
+    public function __construct(PaperMigration $paperMigration, EntityDirCollector $entityDirCollector)
     {
         $this->paperMigration = $paperMigration;
         $this->entityDirCollector = $entityDirCollector;
-        $this->env = $env;
     }
 
     public function getName(): string
@@ -58,9 +54,6 @@ class DatabaseSyncCommand implements CommandInterface
     {
         $io = ConsoleOutput::create($output);
         $verbose = $input->getOptionValue('verbose');
-        if (!$this->isEnabled()) {
-            throw new LogicException('This command is only available in `dev` environment.');
-        }
 
         if ($this->entityDirCollector->count() === 0) {
             $suggested = getcwd() . '/src/Entity';
@@ -128,8 +121,4 @@ class DatabaseSyncCommand implements CommandInterface
         $io->success("Database successfully synchronized.");
     }
 
-    private function isEnabled(): bool
-    {
-        return 'dev' === $this->env || 'test' === $this->env;
-    }
 }

+ 2 - 1
src/Mapping/Column/AutoIncrementColumn.php

@@ -3,6 +3,7 @@
 namespace Michel\PaperORM\Mapping\Column;
 
 use Attribute;
+use Michel\PaperORM\Tools\IDBuilder;
 use Michel\PaperORM\Types\StringType;
 
 #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
@@ -36,7 +37,7 @@ final class AutoIncrementColumn extends Column
             ));
         }
 
-        $length = strlen($prefix) + $pad;
+        $length = ($prefix ? strlen(IDBuilder::generate($prefix)) : 0) + $pad;
         parent::__construct('', $name, StringType::class, $nullable, null, true, $length);
 
         $this->pad = $pad;

+ 1 - 1
src/Michel/Package/MichelPaperORMPackage.php

@@ -57,7 +57,7 @@ class MichelPaperORMPackage implements PackageInterface
                 return new DatabaseDropCommand($container->get(EntityManagerInterface::class), $container->get('michel.environment'));
             },
             DatabaseSyncCommand::class => static function (ContainerInterface $container) {
-                return new DatabaseSyncCommand($container->get(PaperMigration::class), $container->get(EntityDirCollector::class), $container->get('michel.environment'));
+                return new DatabaseSyncCommand($container->get(PaperMigration::class), $container->get(EntityDirCollector::class));
             }
         ];
     }

+ 15 - 0
src/Query/QueryBuilder.php

@@ -5,6 +5,7 @@ namespace Michel\PaperORM\Query;
 use InvalidArgumentException;
 use LogicException;
 use Michel\PaperORM\Cache\EntityMemcachedCache;
+use Michel\PaperORM\Entity\EntityInterface;
 use Michel\PaperORM\EntityManagerInterface;
 use Michel\PaperORM\Hydrator\ArrayHydrator;
 use Michel\PaperORM\Hydrator\EntityHydrator;
@@ -419,6 +420,20 @@ final class QueryBuilder
         }
 
         foreach ($this->params as $key => $value) {
+            if ($value instanceof EntityInterface) {
+                $value = $value->getPrimaryKeyValue();
+            }elseif (is_iterable($value)) {
+                $values = [];
+                foreach ($value as $k => $v) {
+                    if ($v instanceof EntityInterface) {
+                        $values[$k] = $v->getPrimaryKeyValue();
+                    }else {
+                        $values[$k] = $v;
+                    }
+                }
+                $value = $values;
+            }
+
             $joinQl->setParam($key, $value);
         }