Edit one single entity from multiple bundle - symfony

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

Related

symfony dynamically add translation based on condition

I'm searching for a way to add a translation to an existing translation catalogue during runtime.
I have a working symfony 2.3 application which uses translations in de/en/fr/it and fetches all available translation keys from /Resources/translations/messages..yml.
Now if a user logs in I want to have the possibility to override some of the already loaded labels based on setting for that user (e.g. textfield in DB which holds key-value-pairs).
E.g.
messages.en.yml
company.name.short: Company profile
Usersetting:
company.name.short: Profile for company
I found no way to add/override keys to the existing catalogue or to make them available in twig. Is there a Bundle or a setting or some Symfony magic to get this to work?
You'll probably want to extend Symfony's own translation class for this. This article explains how to do that:
http://www.webtipblog.com/extend-symfony-2-translator-to-log-untranslated-messages-to-a-database/
The key point is to override the "translator.class" parameter in your config, and then point it to your own class that first checks for database overrules and will defer to the symfony default implementation if it cannot find one.

Doctrine2 many repositories for the same entity

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?

How to handle data fixtures in Symfony when there are non-nullable entity relationships?

I'm developing an application in Symfony and am yet to setup some data fixtures - I think it'd about time I got round to doing it so setting up a fresh dev copy is faster. :-)
Using my example below, how would I handle setting the 'category' property on my blog to a category? Assuming a ManyToOne relationship with a category entity.
$blog1 = new Blog();
$blog1->setTitle('A day with Symfony2');
$blog1->setImage('beach.jpg');
$blog1->setAuthor('dsyph3r');
$manager->persist($blog1);
I've gathered from the Symblog tutorial that I include AbstractFixture and the Category entity in my file, but surely the category needs to be created before the blog for this to work? How can I ensure that happens when running my fixtures command?
What is the best way to approach this?
You need to share objects between ordered fixtures — make the categories fixture run before the posts fixture.

Use FOSUserBundle in relation with yml-based Entities

I've started a Symfony2 project from scratch where I then installed FOSUserBundle.
Then, I have written (actually, generated with ORM Designer) some entities that need to have relations between them, and with the User entity.
I have Items belonging to Users, Collections belonging to Users that group Items, and so on.
Since I used FOSUserBundle I only have a basic User class (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md , step 3a) defined using annotations, no config/doctrine folder and no User.yml file in it.
I then created the MyBundle/Resources/config/doctrine folder and added the yml files mentioned above.
When I try to generate the entities with the command-line tool everything works fine: it will create the Entities from my yml files.
However, at this point, trying to load up in browsers the url where the login previously worked (when I only had the FOSUserBundle installed) will throw this error:
MappingException: No mapping file found named
'/var/www/concert/src/X/MyBundle/Resources/config/doctrine/User.orm.yml'
for class 'X\MyBundle\Entity\User'.
Following actions, such as generating the CRUD logic, will not work as long as I have an *.orm.yml file in the config/doctrine folder. If I remove those, CRUD generation will work, but generation of actual mysql tables won't.
Juggling with these gets me to a point where I can also get the tables, but then the actual app doesn't work if I try to use any of the url's where the newly generated CRUD is involved because since the entities are based on yml (which I remove to get things "working") it won't have any mapping knowledge.
Is this inherently wrong? To have yml-based entities in relationship with an User entity based on the FOSUserBundle and still be able to get the nice command-line generation tools?
The problem you describe stems from mixing configuration formats (yaml and I assume annotations). You can easily fix this by ditching the annotations in your models and replacing them with yaml-files like you would do in your own models.
Unfortunately the FOSUserBundle-docs only show you how to use annotations, so here is a quick transformation into yaml format when your X\MyBundle\Entity\User extends FOSUSerBundle's UserEntity:
X\MyBundle\Entity\User:
type: entity
table: fos_user
id:
id:
type: integer
strategy: { generator: "AUTO" }
The remaining stuff is taken care of by FOSUserBundle, as the BaseModel is a mapped-superclass and already describes the stuff in the User.orm.xml, but you could just as well replace the existing values or add additional values just like you would do with your own models.
If you don't use annotations throughout your app, you might also want to disable them in your app/config/config.yml to prevent side effects.

best and right way toorganize bundles in symfony2

I'm a little bit confused how I should organize the bundles in symfony2. In my app I'll need 3 pages:
1- Insert
2- List
3- Update
Which one could be the right and best way to organize my code?
src/cp/AddPageBundle
src/cp/EditPageBundle
src/cp/UpdatePageBundle
OR
In one bundle write 3 different controllers, each one in a different file?
OR
In one bundle, write 3 different actions in one controller file?
I'm really confused with this.
Thanks in advance!
In one bundle, 3 different actions in one controller. There is no need to split this functionality across bundles or controllers.
For example you can edit/list/update User with UserController (insertAction, ListAction, UpdateAction) to deal with the user and for example add another controller (CommentsController) for edit/list/update comments. The same situation can be used for your Page example (add/edit/update)
Optional way is to create folder inside controller folder so that we have even more organized code. For example for create Admin folder for controllers: Admin/ConsoleController, Admin/CategoryController, Admin/PluginController to deal with admin functionality.
Exactly I agree with TroodoN-Mike. Also you should create PageBundle with your Page entity and your fields (publish_date, title, content ect), and execute
app/console generate doctrine:crud PageBundle:Page
Symfony will generate a basic CRUD, but with your insert/list/update
just tried this - command is:
app/console doctrine:generate:crud --entity PageBundle:Page
This starts up a wizard on the command line which will prompt you for any other parameters it needs.
Unfortunately only works if your primary key field is called id and has a getID() method.

Resources