Symfony2, where can I put a global class? - symfony

I am developing a quit simple website, with just some text. The text need to be editable by the user, so I am storing the text in a database. Because of that, i need to access to my Doctrine entity from every bundle, so where do I need to write that entity ?
Thank you

All your entities are accesible across your app if you you use namespaces.
Just create it, for example, at Your\WhateverBundle\Entity\Myentity and in every controller you must add: use Your\WhateverBundle\Entity\Myentity;
I think that's you are looking for, isn't it?

Related

Good practices with entities when creating a bundle

I would like to create a bundle with some entities inside but how is the user of my bundle supposed to add a field to my entities if he wants to?
There is some case where the user may want to add fields to the entities of my bundle but he cannot modify the source files of my bundle in the vendor.
Should I abandon the idea to let a user add fields to my bundle's entities? Or what is the correct way to let him do that.
All you need to do is to suggest to the user in your documenation that they extend your base entity class.
FOS userbundle suggests this, and can be seen here in this tutorial document
It really should be as simple as that.
You could also create a mapped superclass (probably abstract) so the user can extend those if needed. This approach is useful if you have multiple child entity classes from some kind of 'core' parent class which provides standard state or functionality.

Silverstripe Class GridFieldConfig_RelationEditor

How does teh Class GridFieldConfig_RelationEditor in SIlverstripe work with data relations. Why do one need it?
It doesn't work with data relations directly. It's just a shortcut for configuring your gridfield for managing and editing relations of your gridfield. A quick look at the code might shed some light on it. The config items it adds are pretty self explanatory. You might also consult the API Docs for another explantion what this class does for you.
Understanding how the gridfield works is a bit hard in the beginning, but it's very modular and powerful. Out of the box it does pretty nothing but you can do a lot with it when you know how to configure it.
You use the GridFieldConfig_RelationEditor to configure your GridFields. The default configuration for a GridField is using a GridField_RecordEditor configuration... which means, by default you only have the option to edit existing data object (DOs displayed in the GridField). If you switch to the GridFieldConfig_RelationEditor configuration, you'll be able to link already existing data objects to the GridField, or unlink linked DOs in the GridField.

how to feed the ::base.html.twig with datas of database more proprely?

in my symfony application i use many controllers . They render their templates who all extend ::base.html.twig template. I feed these templates with an array of datas for display the dynamic content , thanks to my controllers. But the ::base.html.template need also to receive his array of datas for display 4 pictures in a kind of slider . These datas (url of pictures) come from datas base and are available for all pages in my website so i'am asking myself how to send just one time this array of picture? For moment in every controllers I have to repeat the same code for get the url of pictures from database .
Sorry if my question is not clear
I'd personally move the code that creates these sliders out of the controllers into a service layer, be it a proper service or simply a class you create and use dependency injection you need to access things like the EntityManager within it. Services are fine, but they have an associated overhead.
By moving the code out of the controllers, you have a method you can call from all the controllers and one copy of the code, instead of multiple copies through your controllers.
Rendering controllers within twig is certainly possible, but it seems a bit of a fix for bad design (in my opinion anyway). It's a quick win, but not exactly an efficient one.
Remember, controllers should be thin, not bloated fat with code.
I think the correct way would be to create a Twig extension that will get this data for you.

Symfony2 inject an array of services into a controller from config.yml

How I can inject an array of services from config.yml giving class names (with namespaces) to a controller? I need to run a function from each of this services in the controller. At the moment I use $this->get('service'); in the controller, but I need to make controller independent from services. Is there a way to do this?
Edit
I don't know names and how many services will be injected, though all of them implement an Interface.
Edit2
Well, probably I did not express correctly my thoughts. I have a bundle named Widgets. It has an array of widget names, display widget holders for each widget and with AJAX I get the content and display it. At the moment I have in the Widget controller hardcoded some widget deffinitions (title and id for Ajax) and some are retrieved by calling getWidgetList from some controllers from another bundle. Well I need that the list of the widgets wont be hardcoded itself in the widget bundle. I need a way to pass this list from the config.yml. Any ideas?
Injecting an array of services is, generally speaking, not the right approach (even if there was a way to do it, which I don't think there is)
The whole reason you don't want to write container-dependent code is to keep your codebase flexible, lithe, and testable. A variable array of services is, in practice, just a mini container, so if you implemented that you'd just be shrinking the scope of the problem, not eliminating it. You'd still be making your code dependent on an arbitrary bucket of services.
I strongly recommend explicitly defining each service your controllers need (as outlined by the links in the comments from Rufinus and Cerad) or look into using something like the jms/di-extra-bundle.
Update
Maybe you need to do more research on the configuration options available?
How to Create Friendly Configuration for a Bundle
The Config Component
http://symfony.com/doc/current/cookbook/bundles/configuration.html

Symfony2 Using an Entity Manager within Twig extension

I'm creating a menu system using the Tree Doctrine extenion and I want to create a Twig extension to display the menu based on the requested parent node e.g. {% display_menu(side_menu) %}. This function will be in the base twig template (i.e. the menu is on every page of the website).
As I'll be storing the Menu structure with Doctrine, I thought I'd need to access the MenuRepository within the Twig extension so the first problem I came across was getting an Entity Manager into it. When looking for a solution, a few people have said that this is bad practice, which makes sense as a Twig extension is part of the View.
So although there is a solution (linked in similar questions) to my problem, my question is, is there a way I can accomplish it using good practice? Or is there a better way of doing it in the first place?
Making entities aware of any services — including entity managers — is a bad practice. There's nothing wrong about injecting an EM into a Twig extension. Although, I'd rather inject a model manager to a Twig extension, so that the extension is not aware of the persistence layer — it's the job of the manager layer.
So, I'd have MenuManager that's aware of repositores/entity managers and inject it to an extension.

Resources