Skip to content

Commit

Permalink
Adding a meaningful error message if a table has no primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Dec 6, 2017
1 parent 4f6e3ec commit f464f6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Utils/BeanDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ public function getExposedProperties(): array
*/
private function getProperties(Table $table)
{
if ($table->getPrimaryKey() === null) {
// Security check: a table MUST have a primary key
throw new TDBMException(sprintf('Table "%s" does not have any primary key', $table->getName()));
}

$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
if ($parentRelationship) {
$parentTable = $this->schema->getTable($parentRelationship->getForeignTableName());
Expand Down
10 changes: 10 additions & 0 deletions tests/Utils/BeanDescriptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

use Doctrine\Common\Cache\VoidCache;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
use TheCodingMachine\TDBM\TDBMException;
use TheCodingMachine\TDBM\TDBMSchemaAnalyzer;

class BeanDescriptorTest extends TDBMAbstractServiceTest
Expand Down Expand Up @@ -86,6 +88,14 @@ public function testGetTable()
$this->assertSame($usersTable, $beanDescriptor->getTable());
}

public function testTableWithNoPrimaryKey()
{
$table = new Table('no_primary_key');
$this->expectException(TDBMException::class);
$this->expectExceptionMessage('Table "no_primary_key" does not have any primary key');
new BeanDescriptor($table, 'Foo\\Bar', 'Foo\\Generated\\Bar', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, new DefaultNamingStrategy());
}

/*public function testGeneratePhpCode() {
$usersTable = $this->schema->getTable("users");
$beanDescriptor = new BeanDescriptor($usersTable, $this->schemaAnalyzer, $this->schema);
Expand Down

0 comments on commit f464f6a

Please sign in to comment.