Skip to content

Commit aabf2b2

Browse files
committed
allow entity objects as QueryBuilder params and relax SyncCommand env check
1 parent 5827e94 commit aabf2b2

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

src/Command/DatabaseSyncCommand.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@ class DatabaseSyncCommand implements CommandInterface
1818

1919
private EntityDirCollector $entityDirCollector;
2020

21-
private ?string $env;
22-
2321
/**
2422
* @param PaperMigration $paperMigration
2523
* @param EntityDirCollector $entityDirCollector
26-
* @param string|null $env
2724
*/
28-
public function __construct(PaperMigration $paperMigration, EntityDirCollector $entityDirCollector, ?string $env = null)
25+
public function __construct(PaperMigration $paperMigration, EntityDirCollector $entityDirCollector)
2926
{
3027
$this->paperMigration = $paperMigration;
3128
$this->entityDirCollector = $entityDirCollector;
32-
$this->env = $env;
3329
}
3430

3531
public function getName(): string
@@ -58,9 +54,6 @@ public function execute(InputInterface $input, OutputInterface $output): void
5854
{
5955
$io = ConsoleOutput::create($output);
6056
$verbose = $input->getOptionValue('verbose');
61-
if (!$this->isEnabled()) {
62-
throw new LogicException('This command is only available in `dev` environment.');
63-
}
6457

6558
if ($this->entityDirCollector->count() === 0) {
6659
$suggested = getcwd() . '/src/Entity';
@@ -128,8 +121,4 @@ public function execute(InputInterface $input, OutputInterface $output): void
128121
$io->success("Database successfully synchronized.");
129122
}
130123

131-
private function isEnabled(): bool
132-
{
133-
return 'dev' === $this->env || 'test' === $this->env;
134-
}
135124
}

src/Mapping/Column/AutoIncrementColumn.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Michel\PaperORM\Mapping\Column;
44

55
use Attribute;
6+
use Michel\PaperORM\Tools\IDBuilder;
67
use Michel\PaperORM\Types\StringType;
78

89
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
@@ -36,7 +37,7 @@ public function __construct(
3637
));
3738
}
3839

39-
$length = strlen($prefix) + $pad;
40+
$length = ($prefix ? strlen(IDBuilder::generate($prefix)) : 0) + $pad;
4041
parent::__construct('', $name, StringType::class, $nullable, null, true, $length);
4142

4243
$this->pad = $pad;

src/Michel/Package/MichelPaperORMPackage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function getDefinitions(): array
5757
return new DatabaseDropCommand($container->get(EntityManagerInterface::class), $container->get('michel.environment'));
5858
},
5959
DatabaseSyncCommand::class => static function (ContainerInterface $container) {
60-
return new DatabaseSyncCommand($container->get(PaperMigration::class), $container->get(EntityDirCollector::class), $container->get('michel.environment'));
60+
return new DatabaseSyncCommand($container->get(PaperMigration::class), $container->get(EntityDirCollector::class));
6161
}
6262
];
6363
}

src/Query/QueryBuilder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use InvalidArgumentException;
66
use LogicException;
77
use Michel\PaperORM\Cache\EntityMemcachedCache;
8+
use Michel\PaperORM\Entity\EntityInterface;
89
use Michel\PaperORM\EntityManagerInterface;
910
use Michel\PaperORM\Hydrator\ArrayHydrator;
1011
use Michel\PaperORM\Hydrator\EntityHydrator;
@@ -419,6 +420,20 @@ public function buildSqlQuery(): JoinQL
419420
}
420421

421422
foreach ($this->params as $key => $value) {
423+
if ($value instanceof EntityInterface) {
424+
$value = $value->getPrimaryKeyValue();
425+
}elseif (is_iterable($value)) {
426+
$values = [];
427+
foreach ($value as $k => $v) {
428+
if ($v instanceof EntityInterface) {
429+
$values[$k] = $v->getPrimaryKeyValue();
430+
}else {
431+
$values[$k] = $v;
432+
}
433+
}
434+
$value = $values;
435+
}
436+
422437
$joinQl->setParam($key, $value);
423438
}
424439

0 commit comments

Comments
 (0)