Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix applying target column options on foreign key columns #6833

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions lib/Doctrine/ORM/Tools/SchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,19 +439,7 @@ private function gatherColumn($class, array $mapping, Table $table)
$options['columnDefinition'] = $mapping['columnDefinition'];
}

if (isset($mapping['options'])) {
$knownOptions = array('comment', 'unsigned', 'fixed', 'default');

foreach ($knownOptions as $knownOption) {
if (array_key_exists($knownOption, $mapping['options'])) {
$options[$knownOption] = $mapping['options'][$knownOption];

unset($mapping['options'][$knownOption]);
}
}

$options['customSchemaOptions'] = $mapping['options'];
}
$this->gatherColumnOptions($options, $mapping);

if ($class->isIdGeneratorIdentity() && $class->getIdentifierFieldNames() == array($mapping['fieldName'])) {
$options['autoincrement'] = true;
Expand Down Expand Up @@ -661,9 +649,7 @@ private function gatherRelationJoinColumns(
$columnOptions['notnull'] = !$joinColumn['nullable'];
}

if (isset($fieldMapping['options'])) {
$columnOptions['options'] = $fieldMapping['options'];
}
$this->gatherColumnOptions($columnOptions, $fieldMapping);

if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) {
$columnOptions['length'] = $fieldMapping['length'];
Expand Down Expand Up @@ -871,4 +857,21 @@ public function getUpdateSchemaSql(array $classes, $saveMode = false)

return $schemaDiff->toSql($this->platform);
}

private function gatherColumnOptions(array &$options, array $mapping)
{
if (isset($mapping['options'])) {
$knownOptions = array('comment', 'unsigned', 'fixed', 'default');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes sense to copy the column comment from the PK to the foreign key. So that should probably be excluded, but makes it kinda more ugly.


foreach ($knownOptions as $knownOption) {
if (array_key_exists($knownOption, $mapping['options'])) {
$options[$knownOption] = $mapping['options'][$knownOption];

unset($mapping['options'][$knownOption]);
}
}

$options['customSchemaOptions'] = $mapping['options'];
}
}
}
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/Mocks/HydratorMockStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(array $resultSet)
*
* @return array
*/
public function fetchAll($fetchStyle = null, $columnIndex = null, array $ctorArgs = null)
public function fetchAll($fetchStyle = null, $columnIndex = null, $ctorArgs = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was all those unrelated stuff changed?

{
return $this->_resultSet;
}
Expand All @@ -53,7 +53,7 @@ public function fetchColumn($columnNumber = 0)
/**
* {@inheritdoc}
*/
public function fetch($fetchStyle = null)
public function fetch($fetchStyle = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
{
$current = current($this->_resultSet);
next($this->_resultSet);
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/Mocks/StatementMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public function setFetchMode($fetchStyle, $arg2 = null, $arg3 = null)
/**
* {@inheritdoc}
*/
public function fetch($fetchStyle = null)
public function fetch($fetchStyle = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
{
}

/**
* {@inheritdoc}
*/
public function fetchAll($fetchStyle = null)
public function fetchAll($fetchStyle = null, $fetchArgument = null, $ctorArgs = null)
{
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Models/Forum/ForumCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ForumCategory
{
/**
* @Column(type="integer")
* @Column(type="guid", options={"fixed":true, "collation":"latin1_bin", "foo":"bar"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guid might not be a string so applying fixed or collation might not make sense in this case. maybe just use a string simply.

* @Id
*/
private $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ public function getMethodParameters()
*/
public function testAllMethodCallsAreDelegatedToTheWrappedInstance($method, array $parameters)
{
$returnedValue = null;

$stub = $this->wrapped
->expects($this->once())
->method($method)
->will($this->returnValue('INNER VALUE FROM ' . $method));
->willReturnCallback(function () use ($method, &$returnedValue) {
$returnedValue = 'INNER VALUE FROM '.$method;
});

call_user_func_array(array($stub, 'with'), $parameters);
call_user_func_array(array($this->decorator, $method), $parameters);

$this->assertSame('INNER VALUE FROM ' . $method, call_user_func_array(array($this->decorator, $method), $parameters));
$this->assertSame('INNER VALUE FROM ' . $method, $returnedValue);
}
}
33 changes: 33 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/SchemaToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,39 @@ public function testPassColumnDefinitionToJoinColumn()
$this->assertEquals($customColumnDef, $table->getColumn('avatar_id')->getColumnDefinition());
}

public function testPassColumnOptionsToJoinColumn()
{
$em = $this->_getTestEntityManager();
$schemaTool = new SchemaTool($em);

$category = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumCategory');
$board = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumBoard');

$classes = array($category, $board);

$schema = $schemaTool->getSchemaFromMetadata($classes);

$this->assertTrue($schema->hasTable('forum_categories'));
$this->assertTrue($schema->hasTable('forum_boards'));

$tableCategory = $schema->getTable('forum_categories');
$tableBoard = $schema->getTable('forum_boards');

$this->assertTrue($tableBoard->hasColumn('category_id'));

$this->assertSame(
$tableCategory->getColumn('id')->getFixed(),
$tableBoard->getColumn('category_id')->getFixed(),
'Target and source columns have different value of option `fixed`'
);

$this->assertEquals(
$tableCategory->getColumn('id')->getCustomSchemaOptions(),
$tableBoard->getColumn('category_id')->getCustomSchemaOptions(),
'Target and source columns have different schema options'
);
}

/**
* @group DDC-283
*/
Expand Down