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
Related
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
I'm extending the functionality of an existing Symfony-based application and have found that additional bundles and using bundle inheritance has worked well to customise the functionality.
I've now found myself wanting to inherit from an existing bundle to customise its functionality but I also need to add some additional entities and extend the existing ones. The documentation suggests it might not be possible to extend an existing one since the entity is not a mapped superclass.
Is there anything I can do in this case? I basically want to retain the vast majority of the functionality of the existing bundle, but be able to collect more data.
EDIT:
I've looked into Doctrine inheritance and everything seems to require I have access to the top-level class in order to extend it. Which isn't ideal, as I'd like to keep that class the same.
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 !
I cant find any satisfactory solution for the problems which I sometimes meet. For example we have Article, Photo and Comment entities, I want to make Article and Photo commentable. So, there are same approaches:
1. For all entities like Article, Photo make specific enitity like CommentPhoto, CommentArticle. But it makes me crazy when I duplicate the same code...
2. Use mappedSuperClass. But it forces me to extends class like Article - the problem occurs, when i want make Articles for example 'likeable' and I cant do that because i can extend only one class. There are traits which would be helpful but they are available since php5.4.0.
3. So i want to use interface "Commentable" and implement it entities. Then i want to Create entity with relation to this interface. I know there is something like "resolve target entities" (http://symfony.com/doc/master/cookbook/doctrine/resolve_target_entity.html) it allows to configure relationship with interface but this configuration is static (Correct me if I am wrong -> maybe I could configure in config.yml more than one entity for the same interface ?? )
So my question is: Is there any other approach? Or how to use "resolve target entities" to achieve this?
Edit: 4. fourth approach is to add fields 'entity_type', 'entity_id' to Comment, but it is still not clear as I dreamt...
Recently I began to study SF2 but there are some doubts which I'd like to solve:
Let's say that I have a lot (more than 100) of Models which extend one generic model(Entity). Each model has unique functions (rules for parsing content from model source), what is the best way to organize it?
In ZF1 I would create Model/ModelType/ModelName.php file for every model which need customization, in SF2 I'm not sure what kind of entity should I use: One service container which calls entities and call their custom functions (implemented through interface), or maybe one huge Service which has different functions for every single model?
Thanks in advance.
If parsing functions based only on current entity state (use only entity property) it's better to use entity method. Entity aslo can implement some interface, e.g. ParsableInterface.
If paring function need to use another entities it's better to use standalone services handle parsing