-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Move appendParentObject into createNewInstance #7549
Conversation
…instances for which the parent entity will not be managed by sonata
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you Create a 4.x file similar to https://github.com/sonata-project/SonataAdminBundle/blob/4.x/UPGRADE-3.x.md and add some explanation for people overriding the createNewInstance without calling the constructor ?
Wouldnt it be better to open another extension point instead? I dont think it is easy to understand that createNewInstance not only creates it but adds the parent object if it is needed. What about opening the appendParentObject and you can leave it blank if you need to? Not sure if thats better, wdyt? If we go this way we need to add a comment to clarify what this method does exactly. |
@VincentLanglet i've added the 4.x upgrading document |
@jordisala1991 I think that if you do not override the |
I dunno what the best option, and we might need to take some time to find the best one indeed before merging something too fast. I would say that adding a new extension point is a bad idea. We already have 2, the create and the alter. And it could be complicated to understand the difference between appendParentObject and alterNewInstance. IMHO I see two solutions:
|
@VincentLanglet Am I understanding it right that your proposed solution will still need getters/setters in the entity? if yes, i think that it still defeats the propose of trying to avoid anemic domain model 1 2 IMO the solution proposed in this MR is simple fulfills the idea "createNewInstance creates a fully working new instance". making |
👍 The method There is one extension point ( |
getters/setters are not needed in the entity, just a way to access to the value. Using property accessor to accessing a value to display or set any value when editing an object is already the strategy of every Sonata show/edit/... routes. I understood you removed some setters, but I don't see how your entity could work without having getter or public readonly property too.
Your solution might seems good to you because you don't want the appendParent call, but every others users will need to add this call if they override the method for another reason. |
We have docs for this: https://docs.sonata-project.org/projects/SonataAdminBundle/en/4.x/cookbook/recipe_data_mapper/ |
Maybe try/catching the call might solve the issue then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@goetas just curious, how do you deal with required parameters in the constructor of the entities (if they are provided by the form)?
The data mapper works for the props in the form but i need to access the entity from the parent admin, and that it is not easy to do from the from. My entities require in the constructor the things that are used to "identify" somehow the entity. In out application a "user" belongs always to a "company", thus it is not possible to create in any point an user without a company. Also a user can never change company. To enforce this the user entity requires the company entity in the constructor (and we have no setters to set a company). In this way the entity is never in an invalid state (because someone forgot to call the In sonata 3.x we were achieving it by overriding the In 3.x our code was looking as follows. class Company {
/// ....
}
class User {
function __construct (private Company $company);
}
class UserAdmin extends AbstractAdmin {
/// ....
function getNewInstance() {
return new User($this->getParent()->getSubject());
}
} with the change i propose, migrating would be trivial, it is just about changing the method name, so: class UserAdmin extends AbstractAdmin {
/// ....
function createNewInstance() {
return new User($this->getParent()->getSubject());
}
} Without this change we are forced to add setters in all the entities, that will allow to potentially break the entity consistency. |
where would you put the try catch? currently we have no control over |
If I understand correctly the appendParnetObject is throwing an exception, maybe we can add a try/catch in the sonata code around the property accessor to handle case there is nothing to access... |
that is a good question. currently we don't. we try to reduce the number of required parameters only to relations that are required at business-layer. TBH i did not try to solve this case yet, it would be good to find a general solution for it... but i'm not sure if the data mapper has access to the parent admin subject. |
adding a try-catch for this kind of error feels as hiding the error under the carpet. |
I'm not sure about it, it's more about changing the But don't want to be a blocker if @sonata-project/contributors are ok with this solution. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used to override getNewInstance
as well to return null
and be able to control how the models were created with the form mapper, but not anymore, having to relax the constructors to create non-valid entities 😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changelog is also needed in the first message of the PR
Co-authored-by: Vincent Langlet <[email protected]>
thank you! |
Move appendParentObject into createNewInstance to allow creating new instances for which the parent entity will not be managed by sonata
Subject
I am targeting this branch, because i think it is a bug.
Closes #7548
Changelog