Comments (12)
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.
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.
Can you share msgphp config + doctrine orm mapping :)
from symfony-demo-app.
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.
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.
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.
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.
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.
Also An entity should be always valid.
should be the law :)
from symfony-demo-app.
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.
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.
Closing for now. Feel free to re-open with more info :)
from symfony-demo-app.
Related Issues (20)
- Demo Site is down HOT 2
- Switch to Docker
- Could not scan for classes inside "var/phpunit/phpunit-7.5/src/" HOT 1
- Feature Request: Invitations HOT 4
- Add docker elasticsearch container / fix JWT
- Shell for user-bundle demo with bootstrap and jquery HOT 1
- Document Social Media Login HOT 1
- Use API Platform + SF Messenger dedicated
- Ditch src/DataFixtures in favor of yaml fixtures
- Implement ongr-io/ElasticsearchBundle
- Foreign key error on staging servers HOT 1
- Multiple email: forgot password looks for email in users table... HOT 4
- msgphp_user.current.email takes always credential email HOT 4
- Multiple email - forgot password always sends email to primary email HOT 1
- Switch Eav\Attribute to use string identifiers
- Switch Elasticsearch container
- No alive nodes found in your cluster error HOT 6
- Flatten structure HOT 1
- Add Symfony HTTP Responder
- Documentation for implementing email confirmation HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from symfony-demo-app.