I am trying to define a Bundle that helps to index some data (from an Entity) within a sqlite fts table, in order to do some full-text search.
I know I can define alternate connection and Entity Manager using the app config file (like shown in the cookbook), but I was wondering if it was possible to do so within the bundle configuration - by defining, for example, a service with the doctrine service injected, so that it can instantiate a new connection.
Sqlite is not mandatory, it can be any database type (supported by doctrine).
Thanks in advance for your help.
I would suggest looking at FOSUserBundle for how they handle multiple persistence layers (ORM, Couch, MongoDB). You would essentially be doing the same thing. It's just a matter of loading different configurations depending on the driver specified in app/config/config.yml.
As far as i know it it not possible. Also database connections should be managed centralized in app/config.yml.
I would suggest to define the connection there and add a configuration option to you bundle where you can specify wich connection to use for the indexing stuff.
# app/config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: database
host: localhost
user: user
password: secret
driver: pdo_mysql
my_data_index:
dbname: database
driver: pdo_sqlite
# configuration option of you bundle
acme_indexer:
connection: my_data_index
Then in your bundle you can read this config option and obtain the connection/entity manager and inject it into your indexing service.
Edit:
You can for sure create a custom connection programaticly in your bundle
$params = array(
'driver' =>'pdo_sqlite',
....
);
$factory = $container->get('doctrine.dbal.connection_factory')
$connection = $factory->createConnection($params);
But as i said before it is bad practice because you would evade the symfony standart way of defining database connections.
Related
I plan to move my web site to a cloud one. I've been told there is some micro-interruptions in some services.
Therefore I'd like to configure a connection retry for the redis cache set up.
I'm using Symfony 4.2 with this configuration
framework:
cache:
app: cache.adapter.redis
default_redis_provider: '%env(REDIS_URL)%'
Is there any way to configure the retry_interval option of redis in Symfony ?
EDIT : including precisions of #simon.ro
Redis has an option retry_interval which you can specify at connect-time. You should check the documentation of the redis connecting library you are using.
The default_redis_provider is a DSN that will be parsed with parse_url function
So you can add parameters into query string like this :
default_redis_provider: 'redis://u:pass#localhost/2:6379?retry_interval=300&timeout=10&read_timeout=30&tcp_keepalive=10'
path of url /2 is the dbindex of redis
I am using APCu caching for doctine's query and metadata caches.
I know that one can wrap a cache into a TraceableAdapter such that its statistics are shown in the web profiler.
How can I define doctrine's caches to be a TracableAdapter?
Meanwhile the doctrine cache package has been deprecated. The recommended way to use a cache is to define a pool, together with an corresponding doctrine adapter service which can then be used in doctrine's config, see below. By that, the pool automatically shows up nicely in the debug tool bar / profiler.
framework:
cache:
default_redis_provider: 'redis://%env(REDIS_HOST)%/%env(REDIS_DB)%'
pools:
doctrine.metadata_cache_pool:
adapter: cache.adapter.redis
default_lifetime: 0
services:
doctrine.metadata_cache_provider:
class: Symfony\Component\Cache\DoctrineProvider
public: false
arguments:
- '#doctrine.metadata_cache_pool'
doctrine:
orm:
metadata_cache_driver:
type: service
id: doctrine.metadata_cache_provider
Is there a way to configure user roles with SaltStack for MongoDB 3? I see that the mongodb module has relevant role management functions, but the mongodb_user state does not refer to roles anywhere.
Yes, there certainly is!
You'll want to use the Mongodb module, and execute it from a state using module.run.
so, for example, if you want to manage the roles of a user 'TestUser', you'd create 'manage_mongo_roles.sls', and it will contain states like the following:
manage_mongo_roles:
module.run:
- name: mongodb.user_grant_roles
- m_name: TestUser
- roles: ["admin"]
- database: admin
- user: admin
- password: ''
- host: localhost
- port: 27017
The 'name' paramater for the module MUST be prefaced with a m_, so that the state knows to pass this to the module and not use it as the name of the module to be executed.
Also note that the role MUST be of the format
["role"]
The documentation indicates that, if run from the salt CLI it should be contained in single quotes, like so:
'["role"]'
but doing so in the module.run state WILL cause it to fail, and return with a less than descriptive error message.
I'm writing a web-app that will give every client his own database (or schema, since I'm using PostgreSQL) and sub-domain. So what I want is basically:
http://alice.domain.com => uses database/schema alice
http://bob.domain.com => uses database/schema bob
etc...
Of course I don't plan to blindly throw URL parameters around, so after some checking for validity - and given that all my code, schema, etc. etc. are identical for all clients, and the database to connect to is the only difference - is there a way to do this without updating any of the main config files all the time?
I've looked around for some time and found some solutions that don't apply to my scenario because they either assume that different bundles use different databases (I have one bundle using different databases, depending on the client) or that you can simply configure a few different entity managers (I have many clients, not just 5 or 10).
You could include a php file in your imports like..
imports:
...
- { resource: parameters.php }
....
Then in that page use whatever method you use to get the subdomain and deal with that and set the db variables like..
<?php
require_once('a_file_that_generates_these_parameters.php')
$container->setParameter('database_host', $dbHost);
$container->setParameter('database_port', $dbPort);
$container->setParameter('database_name', $dbName);
$container->setParameter('database_user', $dbUser);
$container->setParameter('database_password', $dbPass);
Which would then be useable in your config in the same way the details in parameters.yml currently is. Like..
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
....
Symfony2 uses a Swiftmailer bundle to send emails.
To use and configure Swiftmailer in Symfony2, one has to use such a configuration as explained in the docs, e.g. by using YAML:
swiftmailer:
transport: smtp
encryption: ssl
auth_mode: login
host: smtp.gmail.com
username: your_username
password: your_password
The Swiftmailer is defined in Symfony2 as a service and an instance of it can be obtained in a controller as follows:
$mailerinstance = $this->get('mailer');
Now, let's suppose that two different configurations are required for the Swiftmailer, e.g. one that uses email spooling (e.g. for a scheduled newsletter) and another that sends immediately all the new emails (e.g. for the password lost service). Thus, I guess that two separated instances of the Swiftmailer should be defined. How can I do that in Symfony2?
There is no default symfony way to have 2 different instances. But you can just make a new class that extends swiftmailer, make it to be a service and just pass to the parent constructor your different configuration.
swiftmailer:
default_mailer: second_mailer
mailers:
first_mailer:
# ...
second_mailer:
# ...
// ...
// returns the first mailer
$container->get('swiftmailer.mailer.first_mailer');
// also returns the second mailer since it is the default mailer
$container->get('swiftmailer.mailer');
// returns the second mailer
$container->get('swiftmailer.mailer.second_mailer');
http://symfony.com/doc/current/reference/configuration/swiftmailer.html#using-multiple-mailers