Symfony 2: Creating a service from a Repository

DEPRECATION WARNING: No more factory_service and factory_method. This is how you should do it since Symfony 2.6 (for Symfony 3.3+ check below): parameters: entity.my_entity: “AppBundle:MyEntity” services: my_entity_repository: class: AppBundle\Repository\MyEntityRepository factory: [“@doctrine”, getRepository] arguments: – %entity.my_entity% The new setFactory() method was introduced in Symfony 2.6. Refer to older versions for the syntax for factories prior to … Read more

How does the login check_path route work without default controller/action?

The check_path route/path is used by your firewall to catch login requests. This route’s action is never really accessed. It’s the route/url your login form posts to and the request should be processed by your firewall’s provider service. If the check_path route’s action is being executed there is something wrong with the firewall (the request … Read more

FOS bundle – How to select users with a specific role?

Just add this in your UserRepository or replace $this->_entityName by YourUserBundle:User: /** * @param string $role * * @return array */ public function findByRole($role) { $qb = $this->_em->createQueryBuilder(); $qb->select(‘u’) ->from($this->_entityName, ‘u’) ->where(‘u.roles LIKE :roles’) ->setParameter(‘roles’, ‘%”‘.$role.'”%’); return $qb->getQuery()->getResult(); } If you are using FOSUser Groups you should use: /** * @param string $role * * … Read more

How can I deploy Symfony in a subdirectory?

Here I wrote exactly about that: https://www.refactory-project.com/install-symfony-app-in-a-subfolder-of-an-existing-site/ Upload the application part Start by uploading the application folders at the same level of your site root: [ftproot] — public_html —- … —- … — symfonyapp —- app —- bin —- src —- vendor —- web —— app.php —— app_dev.php —— … —- composer.json —- composer.lock Move … Read more

How to inject the @request into a service?

In Symfony 2.4, this has changed. Now, you can inject the ‘request_stack’ service. For example: use Symfony\Component\HttpFoundation\RequestStack; class MyService { protected $request; public function setRequest(RequestStack $request_stack) { $this->request = $request_stack->getCurrentRequest(); } } In your config.yml: services: my.service: class: Acme\DemoBundle\MyService calls: – [setRequest, [“@request_stack”]] Full documentation is here: http://symfony.com/blog/new-in-symfony-2-4-the-request-stack

The EntityManager is closed

My solution. Before doing anything check: if (!$this->entityManager->isOpen()) { $this->entityManager = $this->entityManager->create( $this->entityManager->getConnection(), $this->entityManager->getConfiguration() ); } All entities will be saved. But it is handy for particular class or some cases. If you have some services with injected entitymanager, it still be closed.

Get current URL in Twig template?

{{ path(app.request.attributes.get(‘_route’), app.request.attributes.get(‘_route_params’)) }} If you want to read it into a view variable: {% set currentPath = path(app.request.attributes.get(‘_route’), app.request.attributes.get(‘_route_params’)) %} The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.

Symfony 2 : multiple and dynamic database connection

If you use ConnectionFactory, your event subscribers attached to the connection will stop working, for example stofDoctrineExtensions. Here is my method. I have as with ConnectionFactory have empty connection and EntityManager. While working I just replace connection configuration by Reflections. Works on SF 2.0.10 😉 class YourService extends ContainerAware { public function switchDatabase($dbName, $dbUser, $dbPass) … Read more