GithubHelp home page GithubHelp logo

Comments (12)

ro0NL avatar ro0NL commented on June 1, 2024

Can you try

-$this->id = $id;
+$this->id = $id->isEmpty() ? null : (int) $id->toString();

In your case the ID property value is always the primitive type (int or null), you hydrate it as UserIdInterface type from getId().

from symfony-demo-app.

crabnky avatar crabnky commented on June 1, 2024

Did not help. :(
I'm not sure if this is related to your bundle - it was working fine some time ago, and last time I did not make any changes to the users. Strange.

from symfony-demo-app.

ro0NL avatar ro0NL commented on June 1, 2024

Can you share msgphp config + doctrine orm mapping :)

from symfony-demo-app.

ro0NL avatar ro0NL commented on June 1, 2024

Also perhaps put a dump($user) before and after $entityManager->flush(); to see if anything unexpected happens with the ID value.

from symfony-demo-app.

crabnky avatar crabnky commented on June 1, 2024

Here is the dump of user before: $entityManager->flush();:

User {#3161 ▼
  -id: null
  -firstName: "Test"
  -lastName: "Test"
  -abbreviation: null
  -birthDate: null
  -phoneExtension: null
  -language: Language {#2006 ▼
    +__isInitialized__: true
    -code: "de"
    -name: "Deutsch"
    -sortOrder: 1
     …2
  }
  -roles: ArrayCollection {#3210 ▼
    -elements: []
  }
  -settings: ArrayCollection {#3165 ▼
    -elements: []
  }
  -imageFile: null
  -imageName: null
  -imageSize: null
  -enabled: false
  -credential: EmailPassword {#8095 ▼
    -email: "[email protected]"
    -password: "$2y$13$PBozBPUemeKghIg8c3Q1sO6oj8MGi93zp5PqMf1x4ngPyuPAWb.F."
  }
  -passwordResetToken: null
  -passwordRequestedAt: null
  -confirmationToken: null
  -confirmedAt: null
  -lastUpdatedAt: null
}

And after:

User {#3161 ▼
  -id: 33
  -firstName: "Test"
  -lastName: "Test"
  -abbreviation: null
  -birthDate: null
  -phoneExtension: null
  -language: Language {#2006 ▼
    +__isInitialized__: true
    -code: "de"
    -name: "Deutsch"
    -sortOrder: 1
     …2
  }
  -roles: PersistentCollection {#8188 ▼
    -snapshot: []
    -owner: User {#3161}
    -association: array:15 [ …15]
    -em: EntityManager {#1298 …11}
    -backRefFieldName: "user"
    -typeClass: ClassMetadata {#1623 …}
    -isDirty: false
    #collection: ArrayCollection {#3210 ▼
      -elements: []
    }
    #initialized: true
  }
  -settings: PersistentCollection {#8189 ▼
    -snapshot: []
    -owner: User {#3161}
    -association: array:15 [ …15]
    -em: EntityManager {#1298 …11}
    -backRefFieldName: "user"
    -typeClass: ClassMetadata {#1829 …}
    -isDirty: false
    #collection: ArrayCollection {#3165 ▼
      -elements: []
    }
    #initialized: true
  }
  -imageFile: null
  -imageName: null
  -imageSize: null
  -enabled: false
  -credential: EmailPassword {#8095 ▼
    -email: "[email protected]"
    -password: "$2y$13$PBozBPUemeKghIg8c3Q1sO6oj8MGi93zp5PqMf1x4ngPyuPAWb.F."
  }
  -passwordResetToken: null
  -passwordRequestedAt: null
  -confirmationToken: null
  -confirmedAt: null
  -lastUpdatedAt: null
}

As you can see the id of user after flush is set to 33. The problem is that in the database the id of new user is 32.

The config\packages\msgphp_user.yaml file:

msgphp_user:
    class_mapping:
        MsgPhp\User\Entity\User: App\Entity\User
    default_id_type: integer

The xml with doctrine mapping:

<entity name="App\Entity\User" table="tbl_users" repository-class="App\Repository\UserRepository">

    <id name="id" column="id_users" type="integer">
        <generator strategy="IDENTITY"/>
    </id>

    <field name="firstName" column="first_name" type="string" length="50" nullable="true"/>
    <field name="lastName" column="last_name" type="string" length="50" nullable="true"/>
    <field name="abbreviation" column="abbreviation" type="string" length="3" nullable="true"/>
    <field name="birthDate" column="birth_date" type="date" nullable="true"/>
    <field name="phoneExtension" column="phone_extension" type="string" length="10" nullable="true"/>
    <field name="imageName" column="image_name" type="string" length="155" nullable="true"/>
    <field name="imageSize" column="image_size" type="integer" nullable="true"/>

    <many-to-one field="language" target-entity="App\Entity\Language" fetch="EAGER">
        <join-column name="lc" referenced-column-name="lc" />
    </many-to-one>

    <one-to-many field="roles" target-entity="App\Entity\UserRole" mapped-by="user" orphan-removal="true" fetch="EAGER">
        <cascade>
            <cascade-persist/>
            <cascade-remove/>
        </cascade>
    </one-to-many>

    <one-to-many field="settings" target-entity="App\Entity\UserSetting" mapped-by="user" orphan-removal="true">
        <cascade>
            <cascade-persist/>
            <cascade-remove/>
        </cascade>
    </one-to-many>
</entity>

Hope it helps. :)

from symfony-demo-app.

ro0NL avatar ro0NL commented on June 1, 2024

Weird.. i'd say Doctrine/SQL at this point is responsible for the generated ID value =/

Could it be some sequence is corrupt or so on SQL's side?

Does the form mess things up maybe? i.e. the empty_data option causing a record in between.

Moreover, you dont really need data_class' => User::class,, IMHO it's simpler to do e.g.

if ($userForm->isSubmitted() && $userForm->isValid()) {
   $user = new User(new UserId(), $form->get('email')->getData(), ...);
}

nevertheless it should work i guess :)

from symfony-demo-app.

crabnky avatar crabnky commented on June 1, 2024

Thanks, this helped - I removed empty_data and data_class from the form and now it is working.

The nice feature of the previous solution was that I could use $user = $userForm->getData(); to load all date from the form - now I have to add each property manually - for example: $user->setFirstName($userForm->get('firstName')->getData());.

Anyway this is strange - and I'am sure it was working some time ago.

By the way, I found this good article: https://blog.martinhujer.cz/symfony-forms-with-request-objects/, so maybe this is the time to make some changes... :)

Thanks again.

from symfony-demo-app.

ro0NL avatar ro0NL commented on June 1, 2024

Cool :) separating layers is good yes 👍

Moreover, you could simplify things even further, using msgphp code ;)

// DomainObjectFactoryInterface $factory
$userId = $factory->identify(User::class, 1);
$user = $factory->create(User::class, ['constructor_arg' => 'value']);

Or

// DomainMessageBusInterface $bus
$bus->dispatch(new CreateUserCommand(['constructor_arg' => 'value']));

// mapping form fields against constructor args, and you're down to
$bus->dispatch(new CreateUserCommand($form->getData());

$bus->dispatch(new EnableUserCommand($userId));

The latter example assumes you know the user id upfront, or is queried separately.

Just showing you various flavors to do things ;)

Can we close? Should we investigate the form issue further, i.e. do we understand what happened :P

from symfony-demo-app.

ro0NL avatar ro0NL commented on June 1, 2024

Also An entity should be always valid. should be the law :)

from symfony-demo-app.

crabnky avatar crabnky commented on June 1, 2024

To be honest I don't understand what is happening - now when adding new user with roles I'm getting error when saving roles:

An exception occurred while executing 'INSERT INTO tbl_users_userroles (id_users, id_userroles) VALUES (?, ?)' with params [36, 5]:

and the id of new user is... 37, so now the id used for query is DECREASED by one. WTF? LOL. :)

Not sure if this is related to your code - rather to doctrine or MSSQL server? Not sure, but I think we may close this issue.

from symfony-demo-app.

ro0NL avatar ro0NL commented on June 1, 2024

My first guess it's related to MSSQL, messing up identities. Perhaps related to the IDENTITY definition? After reading https://www.databasejournal.com/features/mssql/article.php/3307541/Getting-the-Wrong-Identity-in-Microsoft-SQL-Server-identity-Columns.htm a bit :)

from symfony-demo-app.

ro0NL avatar ro0NL commented on June 1, 2024

Closing for now. Feel free to re-open with more info :)

from symfony-demo-app.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.