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 for #6393 - use field names when extracting identifier fields #6394

Closed
wants to merge 9 commits into from
Closed

Fix for #6393 - use field names when extracting identifier fields #6394

wants to merge 9 commits into from

Conversation

tobiasstadler
Copy link

@tobiasstadler tobiasstadler commented Apr 12, 2017

Fixes #6393

Copy link
Member

@lcobucci lcobucci left a comment

Choose a reason for hiding this comment

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

Also we need tests 😄

$columnName = $this->quoteStrategy->getColumnName($versionField, $versionedClass, $this->platform);

$where = array();
Copy link
Member

Choose a reason for hiding this comment

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

Please use short array syntax

$columnName = $this->quoteStrategy->getColumnName($versionField, $versionedClass, $this->platform);

$where = array();
$params = array();
Copy link
Member

Choose a reason for hiding this comment

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

Please use short array syntax


$where = array();
$params = array();
foreach ($versionedClass->identifier as $idField) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you extract a method from this code?

Copy link
Contributor

@phansys phansys left a comment

Choose a reason for hiding this comment

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

Status: Needs work.

/**
* Determains the identifier columns and values for an entity
*
* @param \Doctrine\ORM\Mapping\ClassMetadata $class
Copy link
Contributor

Choose a reason for hiding this comment

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

This class is already imported, so please use ClassMetadata instead FQCN.

* @param array $id
* @return array
*/
private function determineIdentifierColumnsAndValues($class, array $id)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please declare the type for $class argument?

$this->_em->persist($b);
$this->_em->flush();

$this->assertEquals(1, $b->getVersion());
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, use assertSame() instead.

@tobiasstadler
Copy link
Author

Any chance for getting this in?

@Ocramius
Copy link
Member

Ocramius commented Jun 1, 2017

I think this looks fine, but I don't have my laptop atm. Will try merging later today 👍

@Ocramius Ocramius added this to the 2.6.0 milestone Jun 1, 2017
@Ocramius Ocramius self-assigned this Jun 1, 2017
@Ocramius Ocramius removed their assignment Jun 21, 2017
@Ocramius Ocramius changed the title Fix for #6393 Fix for #6393 - use field names when extracting identifier fields Jun 21, 2017
@Ocramius Ocramius self-assigned this Jun 21, 2017
Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

I went through the patch again and tried applying it locally. There are some major issues that need fixing:

  • only the first association column is considered, whereas the IdentifierFlattener considered all columns
  • the association column is not being quoted
  • overall, the logic in determineIdentifierColumnsAndValues is an identifier lookup, and it should produce a [ $values, $columns, $types] triplet
  • The code in determineIdentifierColumnsAndValues should be abstracted away, or an existing abstraction should be found. The IdentifierFlattener is close to it, but it was designed for the identity map internals, not for column operations.

@Ocramius Ocramius removed their assignment Jun 21, 2017
@Ocramius Ocramius removed this from the 2.6.0 milestone Jun 21, 2017
@tobiasstadler
Copy link
Author

Hi I've tried a different (and hopefully better) approach for solving #6394

$identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity);

$id = [];
foreach ($this->class->identifier as $idField) {
Copy link
Member

@lcobucci lcobucci Nov 26, 2017

Choose a reason for hiding this comment

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

This seems like a workaround to me... as @Ocramius said we should rather use field names everywhere.

Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem possible, because in UnitOfWork::createEntity it passes the whole $row ($data) into the identifier flattener and the hydration works by passing the association data as columns not as fields, because it must support composite primary keys as assocations where using field name as a single name would not be able to represent two ore more columns.

Copy link
Member

Choose a reason for hiding this comment

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

ACtually the other way around, BasicEntityPersister::update used the wrong identifier representation. See #8384

class DDC6393Test extends \Doctrine\Tests\OrmFunctionalTestCase
{

protected function setUp()
Copy link
Member

Choose a reason for hiding this comment

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

: void missing

* Test the the version of an entity can be fetched, when the id field and
* the id column are different.
*/
public function testFetchVersionValueForDifferentIdFieldAndColumn()
Copy link
Member

Choose a reason for hiding this comment

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

: void missing

/**
* Test the the version of an entity can be fetched, when the id field and
* the id column are different.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Please add @group 6393

$this->_em->persist($b);
$this->_em->flush();

$this->assertSame(1, $b->getVersion());
Copy link
Member

Choose a reason for hiding this comment

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

self::assert* is more appropriated since the method is static

*/
private $version;

public function __construct($a, $something)
Copy link
Member

Choose a reason for hiding this comment

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

public function __construct(A $a, string $something)

$this->something = $something;
}

public function getA()
Copy link
Member

Choose a reason for hiding this comment

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

Please this method and make the property public, so that we simplify the entity a bit

return $this->a;
}

public function getSomething()
Copy link
Member

Choose a reason for hiding this comment

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

Please this method and make the property public, so that we simplify the entity a bit

$this->something = $something;
}

public function getVersion()
Copy link
Member

Choose a reason for hiding this comment

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

Please this method and make the property public, so that we simplify the entity a bit

return $this->something;
}

public function setSomething($something)
Copy link
Member

Choose a reason for hiding this comment

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

Please this method and simply change the property on the test

@beberlei beberlei self-assigned this Dec 8, 2020
beberlei added a commit to beberlei/doctrine2 that referenced this pull request Dec 8, 2020
beberlei added a commit to beberlei/doctrine2 that referenced this pull request Dec 8, 2020
beberlei added a commit to beberlei/doctrine2 that referenced this pull request Dec 8, 2020
@greg0ire greg0ire closed this Feb 23, 2021
@greg0ire greg0ire deleted the branch doctrine:master February 23, 2021 08:19
beberlei added a commit that referenced this pull request Apr 1, 2021
…ect values. (#8384)

* [GH-6394] Bugfix: IdentifierFlattener support for association non-object values

* [GH-6394] Bugfix: BasicEntityPersister::update used wrong identifiers for version assignment.

* Exclude MissingNativeTypeHint phpcs rule as 7.4 is not lowest version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants