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

Auditing an entity which uses foreign key as primary key fails #502

Open
StefanGramadnikov opened this issue Jul 14, 2022 · 2 comments
Open

Comments

@StefanGramadnikov
Copy link

StefanGramadnikov commented Jul 14, 2022

Environment

Sonata packages

show

$ composer show --latest 'sonata-project/*'

sonata-project/admin-bundle              3.107.3 4.14.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.23.3  4.14.0 Symfony SonataBlockBundle
sonata-project/cache                     2.2.0   2.2.0  Cache library
Package sonata-project/cache is abandoned, you should avoid using it. No replacement was suggested.
sonata-project/classification-bundle     3.18.1  4.2.0  Symfony SonataClassificationBundle
sonata-project/datagrid-bundle           3.5.0   3.5.0  Symfony SonataDatagridBundle
Package sonata-project/datagrid-bundle is abandoned, you should avoid using it. No replacement was suggested.
sonata-project/doctrine-extensions       1.17.0  1.17.0 Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.35.3  4.3.1  Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/entity-audit-bundle       1.4.0   v2.0.0 Audit for Doctrine Entities
sonata-project/exporter                  2.11.0  2.13.0 Lightweight Exporter library
sonata-project/form-extensions           1.9.0   1.17.1 Symfony form extensions
sonata-project/media-bundle              3.38.1  4.4.0  Symfony SonataMediaBundle
sonata-project/twig-extensions           1.10.0  1.10.0 Sonata twig extensions


Symfony packages

show

$ composer show --latest 'symfony/*'
# Put the result here.

PHP version

$ php -v

PHP 8.1.2 (cli) (built: Jan 26 2022 16:40:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies
    with Xdebug v3.1.3, Copyright (c) 2002-2022, by Derick Rethans

Subject

Auditing entity which uses foreign key as primary key throws exception when entity manager flushes.

Minimal repository with the bug

Steps to reproduce

Implement the following entity structure (or similar):

  1. user
    • id (int, auto_increment)
    • random_field (varchar)
    • random_field_2 (varchar)
  2. table_two_related_to_user (one to one relationship)
    • user_id (int, foreign_key, primary_key)
    • data_field (json)
    • data_field_2 (json)

Add the Entity mapped to the table_two_related_to_user table to the entity_audit.yaml
Generate migration. For me generated migration looks like this:

$this->addSql('CREATE TABLE audit_table_two_related_to_user (user_id INT NOT NULL, rev INT NOT NULL, data_field JSON DEFAULT NULL, data_field_2 JSON DEFAULT NULL, revtype VARCHAR(4) NOT NULL, INDEX rev_268e01b1f7711da269f8c38148c4fe8f_idx (rev), PRIMARY KEY(user_id, rev)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE audit_table_two_related_to_user ADD CONSTRAINT rev_268e01b1f7711da269f8c38148c4fe8f_fk FOREIGN KEY (rev) REFERENCES revisions (id)');

Execute migration. The migration should be successful.
Then perform an update on the Entity mapped to the table table_two_related_to_user

Expected results

When entity manager flushes, no exception is thrown and data is stored in the audit table.

Actual results

When entity manager flushes and exception is thrown: Object of class App\Entity\User could not be converted to int
image

Digging into the code of the bundle I found out that the postFlush method of the LogRevisionsListener executes a piece of code that builds a query.
image

It uses entity manager to gather info about the entity and the field types, but from what I see my case is not covered. The code gets the type of the identifier(primary key) using the entity manager associationMappings which in this case is '1' (integer), because the primary key is a 'relationship', and because of that then it tries to cast to int and fails.

Looking at the code it looks like it should get the type of the associationMappings identity column (a.k.a get the type from the primary key of the relationship), as well it should try to get the value from the associated entity primary key.

@VincentLanglet
Copy link
Member

Hi @StefanGramadnikov

While you debug, did you find what should be written instead of

$types[] = $meta->associationMappings[$idField]['type'];
$params[] = $meta->reflFields[$idField]->getValue($entity);

then ?

@github-actions
Copy link

github-actions bot commented Feb 5, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants