Skip to content

Commit

Permalink
Merge pull request #126 from hafidTCM/fix_get_ref_on_nullable_foreign…
Browse files Browse the repository at this point in the history
…_key

Fixing bug when getRef was called on an object with nullable foreign key
  • Loading branch information
moufmouf committed Nov 2, 2016
2 parents 7e8b07f + 131ef08 commit ad659f0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Mouf/Database/TDBM/AbstractTDBMObject.php
Expand Up @@ -284,6 +284,10 @@ protected function getRef($foreignKeyName, $tableName = null)
{
$tableName = $this->checkTableName($tableName);

if (!isset($this->dbRows[$tableName])) {
return;
}

return $this->dbRows[$tableName]->getRef($foreignKeyName);
}

Expand Down
1 change: 1 addition & 0 deletions tests/Mouf/Database/TDBM/TDBMDaoGeneratorTest.php
Expand Up @@ -1231,5 +1231,6 @@ public function testGetOnAllNullableValues()
$allNullable = new AllNullableBean();
$this->assertNull($allNullable->getId());
$this->assertNull($allNullable->getLabel());
$this->assertNull($allNullable->getCountry());
}
}
6 changes: 4 additions & 2 deletions tests/Mouf/Database/TDBM/TDBMSchemaAnalyzerTest.php
Expand Up @@ -80,8 +80,10 @@ public function testGetIncomingForeignKeys3()
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->dbConnection, $cache, $schemaAnalyzer);

$fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('country');
$this->assertCount(1, $fks);
$this->assertEquals('users', $fks[0]->getLocalTableName());
$this->assertCount(2, $fks);
$tables = [ $fks[0]->getLocalTableName(), $fks[1]->getLocalTableName() ];
$this->assertContains('users', $tables);
$this->assertContains('all_nullable', $tables);
}

public function testGetPivotTableLinkedToTable()
Expand Down
13 changes: 13 additions & 0 deletions tests/sql/tdbmunittest.sql
Expand Up @@ -170,6 +170,7 @@ CREATE TABLE IF NOT EXISTS `users` (
CREATE TABLE IF NOT EXISTS `all_nullable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`label` varchar(255) NULL,
`country_id` int(11) NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Expand Down Expand Up @@ -290,6 +291,12 @@ ADD PRIMARY KEY (`role_id`,`right_label`), ADD KEY `right_label` (`right_label`)
ALTER TABLE `users`
ADD PRIMARY KEY (`id`), ADD KEY `country_id` (`country_id`);

--
-- Indexes for table `all_nullable`
--
ALTER TABLE `all_nullable`
ADD KEY `country_id` (`country_id`);

--
-- Indexes for table `users_roles`
--
Expand Down Expand Up @@ -345,6 +352,12 @@ ALTER TABLE `users`
ADD CONSTRAINT `users_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`id`),
ADD CONSTRAINT `users_ibfk_2` FOREIGN KEY (`id`) REFERENCES `contact` (`id`);

--
-- Constraints for table `users`
--
ALTER TABLE `all_nullable`
ADD CONSTRAINT `all_nullable_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`id`);

--
-- Constraints for table `users_roles`
--
Expand Down

0 comments on commit ad659f0

Please sign in to comment.