Skip to content

Commit e0004d9

Browse files
committed
Composite Pk - getById: Implementation
1 parent 50aad9f commit e0004d9

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/Utils/BeanDescriptor.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,20 @@ class BeanDescriptor implements BeanDescriptorInterface
103103
*/
104104
private $configuration;
105105

106-
public function __construct(Table $table, string $beanNamespace, string $generatedBeanNamespace, string $daoNamespace, string $generatedDaoNamespace, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer, NamingStrategyInterface $namingStrategy, AnnotationParser $annotationParser, CodeGeneratorListenerInterface $codeGeneratorListener, ConfigurationInterface $configuration)
107-
{
106+
public function __construct(
107+
Table $table,
108+
string $beanNamespace,
109+
string $generatedBeanNamespace,
110+
string $daoNamespace,
111+
string $generatedDaoNamespace,
112+
SchemaAnalyzer $schemaAnalyzer,
113+
Schema $schema,
114+
TDBMSchemaAnalyzer $tdbmSchemaAnalyzer,
115+
NamingStrategyInterface $namingStrategy,
116+
AnnotationParser $annotationParser,
117+
CodeGeneratorListenerInterface $codeGeneratorListener,
118+
ConfigurationInterface $configuration
119+
) {
108120
$this->table = $table;
109121
$this->beanNamespace = $beanNamespace;
110122
$this->generatedBeanNamespace = $generatedBeanNamespace;
@@ -779,27 +791,35 @@ public function generateDaoPhpCode(): ?FileGenerator
779791
$class->addMethodFromGenerator($findAllMethod);
780792
}
781793

782-
if (count($primaryKeyColumns) === 1) {
783-
$primaryKeyColumn = $primaryKeyColumns[0];
784-
$primaryKeyPhpType = TDBMDaoGenerator::dbalTypeToPhpType($this->table->getColumn($primaryKeyColumn)->getType());
794+
if (count($primaryKeyColumns) > 0) {
795+
$lazyLoadingParameterName = 'lazyLoading';
796+
$parameters = [];
797+
$parametersTag = [];
798+
$primaryKeyFilter = [];
799+
800+
foreach ($primaryKeyColumns as $primaryKeyColumn) {
801+
if ($primaryKeyColumn === $lazyLoadingParameterName) {
802+
throw new TDBMException('Primary Column name `' . $lazyLoadingParameterName . '` is not allowed.');
803+
}
804+
$phpType = TDBMDaoGenerator::dbalTypeToPhpType($this->table->getColumn($primaryKeyColumn)->getType());
805+
$parameters[] = new ParameterGenerator($primaryKeyColumn, $phpType);
806+
$parametersTag[] = new ParamTag($primaryKeyColumn, [$phpType]);
807+
$primaryKeyFilter[] = "'$primaryKeyColumn' => \$$primaryKeyColumn";
808+
}
809+
$parameters[] = new ParameterGenerator($lazyLoadingParameterName, 'bool', false);
810+
$parametersTag[] = new ParamTag($lazyLoadingParameterName, ['bool'], 'If set to true, the object will not be loaded right away. Instead, it will be loaded when you first try to access a method of the object.');
811+
$parametersTag[] = new ReturnTag(['\\'.$beanClassName]);
812+
$parametersTag[] = new ThrowsTag('\\'.TDBMException::class);
785813

786814
$getByIdMethod = new MethodGenerator(
787815
'getById',
788-
[
789-
new ParameterGenerator('id', $primaryKeyPhpType),
790-
new ParameterGenerator('lazyLoading', 'bool', false)
791-
],
816+
$parameters,
792817
MethodGenerator::FLAG_PUBLIC,
793-
"return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading);",
818+
"return \$this->tdbmService->findObjectByPk('$tableName', [" . implode(', ', $primaryKeyFilter) . "], [], \$$lazyLoadingParameterName);",
794819
(new DocBlockGenerator(
795820
"Get $beanClassWithoutNameSpace specified by its ID (its primary key).",
796821
'If the primary key does not exist, an exception is thrown.',
797-
[
798-
new ParamTag('id', [$primaryKeyPhpType]),
799-
new ParamTag('lazyLoading', ['bool'], 'If set to true, the object will not be loaded right away. Instead, it will be loaded when you first try to access a method of the object.'),
800-
new ReturnTag(['\\'.$beanClassName]),
801-
new ThrowsTag('\\'.TDBMException::class)
802-
]
822+
$parametersTag
803823
))->setWordWrap(false)
804824
);
805825
$getByIdMethod->setReturnType($beanClassName);

tests/TDBMDaoGeneratorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
use TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean;
5151
use TheCodingMachine\TDBM\Test\Dao\Bean\CatBean;
5252
use TheCodingMachine\TDBM\Test\Dao\Bean\CategoryBean;
53-
use TheCodingMachine\TDBM\Test\Dao\Bean\CompositePkBean;
5453
use TheCodingMachine\TDBM\Test\Dao\Bean\CountryBean;
5554
use TheCodingMachine\TDBM\Test\Dao\Bean\DateArrayBean;
5655
use TheCodingMachine\TDBM\Test\Dao\Bean\DogBean;
@@ -70,7 +69,6 @@
7069
use TheCodingMachine\TDBM\Test\Dao\BoatDao;
7170
use TheCodingMachine\TDBM\Test\Dao\CatDao;
7271
use TheCodingMachine\TDBM\Test\Dao\CategoryDao;
73-
use TheCodingMachine\TDBM\Test\Dao\CompositePkDao;
7472
use TheCodingMachine\TDBM\Test\Dao\ContactDao;
7573
use TheCodingMachine\TDBM\Test\Dao\CountryDao;
7674
use TheCodingMachine\TDBM\Test\Dao\DogDao;

0 commit comments

Comments
 (0)