For specific needs, I have to create 2 repositories for the same Entity.
The first repository are in the same bundle with the entity and the second, I have to create it in an other bundle but i want to use the same entity with different methodes.
Any idea how can I do that ?
Can't say I see the point, but try this: Symfony 2: Creating a service from a Repository
Create 2 services which extend EntityRepository, construct them correctly and give them the entity reference.
Obviously $em->getRepository('Entity') won't work.
But $this->container->get('repository_service_1') will.
Why do you need this?
Related
I have a Bundle which is the core of my website (we will name it CoreBundle).
But my website will be share and need to be modulable.
For example, I have an entity CoreApplication :
id
attr1
attr2
But now, I need to add 2 independent Bundle (part1 / part2) to the core, which will be added to the core depends on what the company needs. Both parts will do the same thing: add fields to Application.
For example, the company could install only part1. Or only part2. Or part1 and part2.
That's why I don't know how to start.
I could create one Bundle for each part and extends the Core. But if I add part1Bundle and part2Bundle, this will not work?
I could create an Interface InterfaceApplication and use resolve_target_entities...
How would you do?
Thanks!
This has been done before by OROCRM. Although I would try to avoid going that route unless it is your last resort.
How it works:
You create your bundles
Your bundle has doctrine migrations that updates the database schema
The entities gets populated from the database schema
Documentation:
https://oroinc.com/doc/orocrm/2.0/cookbook/entities/adding-properties
https://oroinc.com/doc/orocrm/2.0/book/entities
EntityExtendBundle Source code:
https://github.com/oroinc/platform/tree/master/src/Oro/Bundle/EntityExtendBundle
I have entity A and entity B which extend entity A.
Entity A - in vendor bundle.
Entity B - in /src bundle.
I search a lot of time and only solution works for me. It's using ClassMetadataListener on Doctrine Event loadClassMetadata.
But extending on entity B not works. I need add all properties form entity A to entity B.
I can't understand why.
You cannot simply extend a class like normally when using Doctrine2.
Check the documentation on chapter 6. Inheritance Mapping on how to implement inheritance on your entity classes.
You are probably interested in chapter 6.1. Mapped Superclasses.
A mapped superclass is an abstract or concrete class that provides persistent entity state and mapping information for its subclasses, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.
For this to work you have to use the #MappedSuperclass annotation to your entity base class.
Add #MappedSuperclass annotation in top of A. That would be enough :)
I am in need of grabbing a few fields of an entity, running them through some processing, and returning the processed data. I am wondering if it is possible to call the getters of the Entity for which the custom repository is being built inside that custom repository? The only way I thought of so far seems like it would create an infinite loop by calling the entity's repository, which would include a call to the very custom repository being used as well.
I can write the actual queries, but I figured it it would be cleaner to access the data through existing methods, then why not?
Update: So I built a method in the custom repository that I pass the entity object to that I want to work with. Seems a little weird that I would have to pass the entity to it's own custom repository so that it would know what to act on, but I can't seem to find any other way. So in my Controller right now I am calling the entity, then calling up the repository for the entity, and passing the previously called entity to the repository. Is that right?
$user = $this->get('security.context')->getToken()->getUser();
$user_repository = $this->getDoctrine()->getRepository('AppBundle:User');
$profiles = $user_repository->getAllUsersProfiles($user);
It turns out I had the whole concept of Custom Repositories incorrect. I was using them to store methods that would pull out data relevant to the entity, but only relevant in the sense that the entity was being used to filter something else.
Prior to this epiphany my User Custom Repository had methods like getAllUsersProfiles($user) and getAllUsersCampaigns($user). My previously incorrect understanding was that because the User entity was the thing that was central to filtering the Profiles and Campaigns, that these belonged in the User Custom Repository.
What I have now come to understand is that getAllUserProfiles($user) belongs in the Profiles Custom Repository, and would be more appropriately named getAllByUser($user). Now when I need to use it, I pull up the Profiles Repository and call the appropriate method:
$profile_repository = $this->getDoctrine()->getRepository('AppBundle:Profile');
$all_profiles = $profile_repository->getAllByUser($user);
I am using FOSUserBundle, SonataUserBundle. Here is my code,
https://github.com/vishalmelmatti/FOSSonataUserFacebookIntegration
I have 3 options to access users table,
1) Create UsersRepository in https://github.com/vishalmelmatti/FOSSonataUserFacebookIntegration/tree/master/src/Application/Sonata/UserBundle/Entity
2) Extend FOSUserBundle's FOS\UserBundle\Doctrine\UserManager and add my methods to it.
3) Extend FOS\UserBundle\Security\UserProvider and add my methods to it which intenally access class created in 2.
What would be the best approach ?
The repository approach sounds to be the best for me, as it's entity related / specific logic on "database" operations, and repositories ARE the way to do it right.
It's quite well explained in the docs
I have in my Symfony 2.1 RC app a simple Comment model (using Doctrine 2). Every comment has a user and a message.
Currently, the CommentBundle manages comments on articles. I'd like it to be more generic to be able to comment any kind of entity without copying code across different bundles dedicated to comments...
For this to work, I also need a way to reference any entity from the comment one. I think having two fields entity_type and entity_id can be a nice solution. However, I can't get the object from these without mapping entity_type to classes manually and using the find method.
So how do I reference an entity from a comment ? And how can I create generic behavior working on several entities ?
You can create a abstract base class entity called Commentable and create entities that inherit Commentable such as Document or Post.
Since Document and Post are derived from Commentable, you can create a one to many relationship between the entities Commentable and Comment respectively.
Make sure to include in your base class ORM annotations for inheritance:
#InheritanceType
#DiscriminatorColumn
#DiscriminatorMap
Examples can be found on Doctrine Project Inheritance Documentation