I'm trying to create a reusable bundle for Symfony projects.
This bundle has a huge model mapping, but I encounter a conceptual problem :
How, with Symfony, can I bind one of my entities to the user entity of the final application of the people using the bundle ?
Can I use some configuration to get the user class and dynamically create the mapping between those two classes or is there a better approach of this matter ?
Thank you community :) !
In fact I wanted something like this : https://www.theodo.fr/blog/2013/11/dynamic-mapping-in-doctrine-and-symfony-how-to-extend-entities/
This is surely the solution I will apply to my project.
After a few days of developement, I noticed that for each new entity I create, I must map it to a "end-user extendable" class of the bundle. If I tell the end-user to map himself the Project class to his own User class, I later have to tell him "Each class which is mapped to the Project class must be also manually mapped".
I think none of us wants to implement an external bundle and override each part of its internal model. Dynamic mapping with event subscribers avoids it !
Ty for your help, I hope this note will help some other lads :D !
Related
Our team is having a bit of a headache while modifying our core model.
Our problem :
We have a bundle, let's name it COREBundle. We load it via composer on every app to get the very basic required to boot our system.
The COREBundle provides a Customer Entity.
Each app has some special customer properties that we want to add : how to proceed ?
Of course the complexity here is to make everything work with the COREBundle provided.
We are exploring the idea of having some MappedSuperClasses but it means we will need to modify all our entities in ALL our apps, which will be extremely time-consuming.
Help? Thank you!
I am running a Symfony 3.4 based web service using Doctrine to manage and persist the different data entities.
Now I am trying to implement a method which transfers older, abandoned user accounts to another database which acts as archive.
Regarding to the Symfony docs it should be no problem to configure Doctrine to manage different database connections and entity managers.
However I do not completely understand the process on how to setup this use case:
Assume the Symfony project has different data entities DataEntity1, DataEntity2, etc. and different infrastructure entities Infrastructure1, etc..
How to tell Doctrine to initialize the archive DB with the data entities only?
How to move the entities between the DBs? Is loading them from entity manager 1 and persisting them in entity manger 2 the correct way?
Is there any best practice on how to do this?
If I understand your question correctly, you should use the prefix option for the mapping configuration.
prefix
A common namespace prefix that all entities of this mapping share.
This prefix should never conflict with prefixes of other defined
mappings otherwise some of your entities cannot be found by Doctrine.
This option defaults to the bundle namespace + Entity, for example for
an application bundle called AcmeHelloBundle prefix would be
Acme\HelloBundle\Entity.
Have a look at https://symfony.com/doc/3.4/reference/configuration/doctrine.html it shoul help you.
To move the entities between the two DBs, you should have two entity managers and use the correct one to persist olders accounts.
Hope this helps.
The case I'm trying to solve is:
I got bundle taking care of orders called 'OrderBundle' and wrote additional budle taking care of complaints - 'ComplaintsBundle' - the bundle is not fully standalone - the
entity 'Complaint' is coupled with the entity 'Order' by the field 'order' inside 'Complaint' and - what I think is the
real coupling problem - by the Doctrine annotation pointing to "Order".
What I'm thinking of and would like to achieve is to write a bundle 'Complaints' which can be standalone or have additional, optional fields which can be configured to couple with different entity. For example - the bundle 'Complaints' can serve as a complaints bundle for any
entity which would eventually need complaints functionality.
The similiar situation I got with other bundles. Another example is 'User' entity from UserBundle which is related to 'Company' entity in CompanyBundle,
but the thing again is that I want the UserBundle to be standalone bundle which can be easily installed among different projects which not necessary need the CompanyBundle but
the User can be attached to another entity/entities. It goes futher because it is not only about doctrine annotations but views, created forms, validation, and many other
involved stuff.
What should be my approach to achieve that? Im quite new to symfony in fact and the idea of standalone reusable bundles is also quite new to me, before
I didn't any bundles but was developing applications as a whole. Also I would like to develop other, not related to my job, open-source bundles to share with others, so
I guess I need to apply to them this attitude of not being coupled to practically nothing else - how that can be achieved practically, can you share your experience, thoughts or point to explanatory articles?
Thank you very much for your guidelines and please take note it's a resonable question as there is a lack of know-how about decoupling bundles in Symfony community.
In your standalone bundles, you should declare your entities as abstract and set the doctrine annotation #MappedSuperclass on it.
Then, in your application, you will have to create your 'final' entities witch will be extend the mapped super class provided by your bundle.
Also, you will probably need to expose the FQCN of childs entities in your bundle configuration.
It can seem a little heavy, but unfortunatly Doctrine mapping is not overridable.
Here is some bundles implementing this solution :
Orbital/CmsBundle
FosUserBundle
To handle relations between your MappedSuperclass You have to use Interface in your relation annotation. Here is the official documentation about it.
Best regards
While Working on my project in Symfony, I realized that there is one entity(Assign Item to category) in my project which is being used in all other entities like products,categories,upsells etc as all of them need to be assigned to a category.
Is there a way that this entity can be reused?
I know a way where it can be defined in all ORM's (copy pasted in all entities), but want a more optimal solution to this.
Any help would be appreciated.
After doing a research, here is what I found.
There is a term called dynamic binding in Symphony where one entity can be used into another there by saving us from writing the same code again and again.
Here is the link that helped me:
https://www.theodo.fr/blog/2013/11/dynamic-mapping-in-doctrine-and-symfony-how-to-extend-entities/
Hope that helps someone.
Create a bundle with abstract classes that defines your entities. Don't forget to put there the annotations. Then load that bundle in your projects and extend your entities from these abstract classes. You can override methods or atributtes to customize to the certain usage. FosUserBundle works in that way
FOSUserBundle usage
Can somebody help me to overwrite FOSUSerBundle in multiple places (Bundles).
I have overwritten in separate bundle named "UserBundle" with user_type=ADMIN but now I need to inherit the same FOSUserBundle in "ClientBundle" with user_type=CLIENT,
Like this I have multiple roles for user one is for backend and other is for frontend and can be more. How I can achieve this?
Please help me out on the above issue.
Thanks in Advance.
You simply can't... The Symfony2 bundle architecture for inheritance is same as PHP, you can't inherit more than once a bundle as same as you can't inherit multiple class in PHP. Sorry but you need to rethink how you will split your application in multiple bundles.