I work with several connexion
Here is my config.yml :
doctrine:
dbal:
default_connection: connexion_1# specify the connexion used by default
connections:
connexion_1:
driver: "%database_driver1%"
....
connexion_2:
driver: "%database_driver2%"
...
I want to inject 2 connexion entity manager in a service.
In my service.yml i can just inject one entity_manager named "#doctrine.orm.entity_manager" . This is the default manager
#doctrine.orm.entity_manager argument
zip_command:
class: BackEndBundle\Command\ZipCommand
arguments:
- '#doctrine.orm.entity_manager'
tags:
- { name: console.command }
How can inject an other connexion ?
Option 1
According to documentation you should be able to inject your custom entity managers via doctrine.orm.{entity_manager_name_here}_entity_manager alias. You can find your entity managers' name under doctrine.orm.entity_managers in config.yml.
Option 2
If you want to have access to all of your entity managers in your service, you could inject the Doctrine\Common\Persistence\ManagerRegistry directly and use getManager(s) methods.
Related
I am working on upgrade Symfony 3.3 to 4.4 and almost done with it, but I have one last issue.
I enabled autowiring with default config, but whole project uses lots of #ParamConverter conversions without annotation.
Problem: ParamConverter with auto_convert tries to convert services and classes that are mentioned to controller by typehinting for autowiring (not entities).
Controller:
/**
* #Route("/report", name="report_page")
*/
public function report(
Request $request,
FileManager $fileManager
): Response {
// code
}
Error:
The class 'App\Service\FileManager' was not found in the chain configured namespaces App\Entity.
Service 'App\Service\FileManager' exists and works correctly.
ParamConverter config (by default 'request: {converters: true, auto_convert: true}'):
sensio_framework_extra:
router:
annotations: false
Doctrine config:
doctrine:
dbal:
default_connection: default
connections:
default:
# config
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
Base:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: App\Entity
Controller setting (services.yaml):
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{DataFixtures,Entity,Objects,Repository,Utils}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
I understand that I can set 'sensio_framework_extra.request.auto_convert' to false and use #ParamConverter annotation everywhere in controllers, but there are too many places with this conversion and I try to get default behavior, so additional #ParamConverter annotations would be extra.
Just in case: I tried to disable auto_convert and everything worked correctly (services were autowired and entities were passed to controllers).
Also I checked that I installed last available versions of packages and bundles.
Need some hints on it. Probably I missed something. I tried to find exact way how ParamConverter works by default and how it checks that class is entity or not, but it is too complicated and I decided that I dug too deep.
EDIT:
Probably I have some issue with autowiring's config and not paramconverter. But I checked php bin/console debug:autowiring and got all needed services as available via autowiring.
It looks like symfony is trying to convert the fileManager like an entity.
Try moving the injected service(s) into the __construct method of the controller (and add the property to the controller) like:
private $fileManager;
...
public function __construct(FileManager $fileManager) {
$this->fileManager = $fileManager;
}
and then access it in the report method:
$this->fileManager
I trying to install SonataAdmin on my Symfony Project but at the end of the part-2 of the documention when i'm trying to go on "http://localhost:8000/admin/" I have a error : "You have requested a non-existent service "admin.category" in . (which is being imported from "C:\wamp64\www\Sonata/app/config\routing.yml"). Make sure there is a loader supporting the "sonata_admin" type."
I have no idea why, i give give my all my parameters code maybe it's can help you to understand my problem.
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
`
The indentation is going wrong i add you a picture of this file. Service code
The sonata admin services must be public. In your config, you have default as public: false and that is why you get this error.
So you have 2 options:
Specify public: true for your admin service (in your example file)
Or the better way: create a new services file (eg admin.yml) where you dont use these defaults (the _defaults key with public: false). Public is true by default, so you don't have to specify that by _defaults. In this case you must import your new file in config.yml to work:
Top of app/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: admin.yml }
app/admin.yml content:
services:
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
I think you did a mistake by writing your category.admin service in: Sonata/app/config/routing.yml,
instead of Sonata/src/YourAdminBundle/Resources/config/services.yml
Run this command on terminal. Because You might have missed installing
php composer.phar require sonata-project/doctrine-orm-admin-bundle
After This Add this code below to your AppKernel.php
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
// set up basic sonata requirements
// ...
new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
// ...
);
}
I am using JMSSerializerBundle with Symfony3. I am using TimestampableEntity trait. What I want to achieve is to receive that trait with JMSSerializer.
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
class Thread
{
use TimestampableEntity;
use SoftDeleteableEntity;
(...)
}
I have added metadata to the jms_serializer config like this:
config.yml
jms_serializer:
enable_short_alias: false # controls if "serializer" service is aliased to jms_serializer.serializer s
metadata:
directories:
- { path: "%kernel.root_dir%/Resources/Gedmo/serializer", namespace_prefix: 'Gedmo\Timestampable\Traits' }
\app\Reources:
Gedmo\Timestampable\Traits\TimestampableEntity:
exclusion_policy: ALL
properties:
created_at:
expose: true
But it is not working.
I know that I can configure it to use my Thread class and expose all necessary fields but I wonder if it is possible for traits.
I want to set Amazon S3 settings from database and not from parameters.yml
can someone point me to right direction on how to use database (doctrine) parameters before symfony2 calls services.
#app/config/config.yml
services:
acme.aws_s3.client:
class: Aws\S3\S3Client
factory_class: Aws\S3\S3Client
factory_method: 'factory'
arguments:
-
key: %amazon_s3.key%
secret: %amazon_s3.secret%
# knp_gaufrette
knp_gaufrette:
adapters:
profile_photos:
aws_s3:
service_id: 'acme.aws_s3.client'
bucket_name: 'myBucket'
options:
directory: 'myDirectory'
acl: 'public-read'
Use your own service factory for acme.aws_s3.client service configuration.
services:
acme.aws_s3.client:
class: Aws\S3\S3Client
factory_class: My\S3ClientFactory
factory_method: createClient
arguments: [ #settings_repository ]
#settings_repository - any service that has access to db. E.g. doctrine entity repositry, or entire object manager.
My\S3ClientFactory::createClient - pretty much the same as native Aws\S3\S3Client::factory except that it would take params from db.
Hope this helpful.
I got this error
MappingException: The class 'Telnet\IPBBridgeBundle\Entity\Member' was not found in the chain configured namespaces Telnet\CSSBundle\Entity
I have 2 bundles with entities and several connections into doctrine config
orm:
default_entity_manager: site
entity_managers:
site:
connection: siteConfig
mappings:
CSSBundle: ~
forum:
connection: forumConfig
mappings:
IPBBridgeBundle: ~
and this into provider
providers:
main:
entity: { class: Telnet\IPBBridgeBundle\Entity\Member, property: username }
What I'm doing wrong?
SecurityBundle use default Entity Manager, and Entity, that implements UserInterface must be handled by default Entity Manager.