Skip to content

Commit 6abd40e

Browse files
authored
Merge pull request #148 from Kharhamel/requiredInFindOne
fixed an issue with required parameters in findOneBy
2 parents 6c1bc9f + 28760a9 commit 6abd40e

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/Utils/BeanDescriptor.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,9 +1159,10 @@ private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespa
11591159
$parameters = [];
11601160
//$functionParameters = [];
11611161
$first = true;
1162+
/** @var AbstractBeanPropertyDescriptor $element */
11621163
foreach ($elements as $element) {
11631164
$parameter = new ParameterGenerator(ltrim($element->getVariableName(), '$'));
1164-
if (!$first) {
1165+
if (!$first && !($element->isCompulsory() && $index->isUnique())) {
11651166
$parameterType = '?';
11661167
//$functionParameter = '?';
11671168
} else {
@@ -1170,7 +1171,7 @@ private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespa
11701171
}
11711172
$parameterType .= $element->getPhpType();
11721173
$parameter->setType($parameterType);
1173-
if (!$first) {
1174+
if (!$first && !($element->isCompulsory() && $index->isUnique())) {
11741175
$parameter->setDefaultValue(null);
11751176
}
11761177
//$functionParameter .= $element->getPhpType();
@@ -1208,7 +1209,7 @@ private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespa
12081209
foreach ($columns as $localColumn => $foreignColumn) {
12091210
// TODO: a foreign key could point to another foreign key. In this case, there is no getter for the pointed column. We don't support this case.
12101211
$targetedElement = new ScalarBeanPropertyDescriptor($foreignTable, $foreignTable->getColumn($foreignColumn), $this->namingStrategy, $this->annotationParser);
1211-
if ($first) {
1212+
if ($first || $element->isCompulsory() && $index->isUnique()) {
12121213
// First parameter for index is not nullable
12131214
$filterArrayCode .= ' '.var_export($localColumn, true).' => '.$element->getVariableName().'->'.$targetedElement->getGetterName()."(),\n";
12141215
} else {

tests/TDBMAbstractServiceTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,10 @@ private static function initSchema(Connection $connection): void
421421
$db->table('albums')
422422
->column('id')->integer()->primaryKey()->autoIncrement()
423423
->column('artist_id')->references('artists')->comment('@JsonCollection(key="discography")')
424-
->column('title')->string();
424+
->column('account_id')->references('accounts')
425+
->column('node_id')->references('nodes')->null()
426+
->column('title')->string()
427+
->then()->unique(['artist_id', 'account_id'])->unique(['artist_id', 'node_id']);
425428

426429
$db->table('tracks')
427430
->column('id')->integer()->primaryKey()->autoIncrement()
@@ -682,6 +685,7 @@ private static function initSchema(Connection $connection): void
682685
self::insert($connection, 'albums', [
683686
'id' => 1,
684687
'artist_id' => 1,
688+
'account_id' => 1,
685689
'title' => 'Animals'
686690
]);
687691

tests/TDBMDaoGeneratorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@
3636
use TheCodingMachine\TDBM\Dao\TestUserDao;
3737
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserDaoInterface;
3838
use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserInterface;
39+
use TheCodingMachine\TDBM\Test\Dao\AlbumDao;
3940
use TheCodingMachine\TDBM\Test\Dao\AllNullableDao;
4041
use TheCodingMachine\TDBM\Test\Dao\AnimalDao;
4142
use TheCodingMachine\TDBM\Test\Dao\ArtistDao;
43+
use TheCodingMachine\TDBM\Test\Dao\Bean\AccountBean;
4244
use TheCodingMachine\TDBM\Test\Dao\Bean\AllNullableBean;
4345
use TheCodingMachine\TDBM\Test\Dao\Bean\AnimalBean;
4446
use TheCodingMachine\TDBM\Test\Dao\Bean\ArrayBean;
4547
use TheCodingMachine\TDBM\Test\Dao\Bean\Article2Bean;
4648
use TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean;
49+
use TheCodingMachine\TDBM\Test\Dao\Bean\ArtistBean;
4750
use TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean;
4851
use TheCodingMachine\TDBM\Test\Dao\Bean\CatBean;
4952
use TheCodingMachine\TDBM\Test\Dao\Bean\CategoryBean;
@@ -2032,6 +2035,33 @@ public function testCanNullifyBlob(): void
20322035
fclose($fp);
20332036
}
20342037

2038+
/**
2039+
* @depends testDaoGeneration
2040+
*/
2041+
public function testOptionnalParametersCanBeNullInFindOneBy()
2042+
{
2043+
$albumDao = new AlbumDao($this->tdbmService);
2044+
$artist = new ArtistBean('Marcel');
2045+
2046+
$albumDao->findOneByArtistAndNode($artist, null);
2047+
$this->assertEquals(1, 1);
2048+
}
2049+
2050+
/**
2051+
* @depends testDaoGeneration
2052+
*/
2053+
public function testRequiredParametersCannotBeNullInFindOneBy()
2054+
{
2055+
$albumDao = new AlbumDao($this->tdbmService);
2056+
$artist = new ArtistBean('Marcel');
2057+
$account = new AccountBean('Jamie');
2058+
2059+
$albumDao->findOneByArtistAndAccount($artist, $account);
2060+
2061+
$this->expectException('TypeError');
2062+
$albumDao->findOneByArtistAndAccount($artist, null);
2063+
}
2064+
20352065
/**
20362066
* @depends testDaoGeneration
20372067
*/

0 commit comments

Comments
 (0)