I'm using SncRedisBundle with success locally but can't have it work using heroku.
Here is my config :
#config.yml
imports:
- { resource: heroku/parameters_heroku.php } #heroku cloud provider configuration's
snc_redis:
clients:
default:
type: predis
alias: default
dsn: "%redis_url%"
cache:
type: predis
alias: cache
dsn: "%redis_url%&database=1"
logging: false
session:
type: predis
alias: session
dsn: "%redis_url%&database=2"
task:
type: predis
alias: task
dsn: "%redis_url%&database=3"
doctrine:
type: predis
alias: doctrine
dsn: "%redis_url%&database=4"
session:
client: session
ttl: 10800 #le session expirera apres 3 heures
doctrine:
metadata_cache:
client: doctrine
entity_manager: default # the name of your entity_manager connection
document_manager: default # the name of your document_manager connection
result_cache:
client: doctrine
entity_manager: [default] # you may specify multiple entity_managers
query_cache:
client: doctrine
entity_manager: default
second_level_cache:
client: doctrine
entity_manager: default
monolog:
client: cache
key: monolog
swiftmailer:
client: default
key: swiftmailer
#heroku/parameters_heroku.php
<?php
$is_heroku = getenv("IS_HEROKU");//manually added : heroku config:set IS_HEROKU=1
if (!$is_heroku) {
return;
}
if ($redisUrl = getenv('REDIS_URL')) {
$aParsedRedisUrl = parse_url($redisUrl);
$redisConstructedDsn = 'redis://'.$aParsedRedisUrl['host'].':'.$aParsedRedisUrl['port'].$aParsedRedisUrl['path'].'?'.'password='.$aParsedRedisUrl['pass'].($aParsedRedisUrl['query'] ? '&'.$aParsedRedisUrl['query'] : ''); // le user ne sert pas : .'&user='.$aParsedRedisUrl['user']
$container->setParameter('redis_url', $redisConstructedDsn);
}
if I dump $redisConstructedDsn during the heroku build process, I've got something like that : redis://xxxxxx.compute-1.amazonaws.com:18839?password=foobar, so, it seem pretty compatible with predis doc
but the composer construction fails with a
remote: [Predis\Response\ServerException] remote:
NOAUTH Authentication required.
I can't find what I did wrong
problem solved :
initially I used this kind of syntax : dsn: "%redis_url%/3" wich is incompatible with heroku.
When I changed it to an acceptable format : "%redis_url%?database=3", I added bug elsewhere, so here is the simple correction in heroku/parameters_heroku.php
if ($redisUrl = getenv('REDIS_URL')) {
$container->setParameter('redis_url', $redisUrl);
}
some resources on the internet will say that predis isn't compatible with heroku's format, but they are old&wrong ;)
Related
In my bundle i create a separate database connection and an EntityManager for it. Everything works fine, except those two things don't show up in the development profiler. There is only the default EntityManager and the default connection.
So basically i created 3 new service definitions for an Doctrine\Common\EventManager, an Doctrine\DBAL\Connection and an Doctrine\ORM\EntityManager. I've already tried to add these new service definition to the ContainerBuilder with the same naming convention which is used by the doctrine bridge, but they still won't show up in the profiler. The connection works fine, but i want debug it with and integrate it in the Symfony lifecycle.
The question is:
What is the best practice to create a separate database connection via Doctrine inside of a Symfony Bundle if the Symfony application is only configured to support one connection?
I believe you should take a look at this doc. They described there how to add another EntityManager, which mean another connection. First step is to create configuration.
Especialy take a look at doctrine.yaml configuration:
# config/packages/doctrine.yaml
doctrine:
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
customer:
# configure these for your database server
url: '%env(DATABASE_CUSTOMER_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Main'
prefix: 'App\Entity\Main'
alias: Main
customer:
connection: customer
mappings:
Customer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Customer'
prefix: 'App\Entity\Customer'
alias: Customer
Above are two entity managers, 'default' and 'customer'. There are also two cennections, one for each manager.
If configuration is valid you will have access to those managers by passing its names to 'getManager' method.
$entityManager = $this->getDoctrine()->getManager('default');
$customerEntityManager = $this->getDoctrine()->getManager('customer');
If you cant edit configuration:
What what about creating custom class (Manager or something) in which you will manually create connection. Take a look at this, it should help you.
getting-a-connection
Recently I am upgrading my application from Symfony 3.4 to Symfony 4.2. I am encountering error telling that read error on connection .
Here is the stack trace:
at var/cache/dev/ContainerRUEbk9e/Redis_ca5fc0f.php:312
at Redis->get('DoctrineNamespaceCacheKey[]')
(var/cache/dev/ContainerRUEbk9e/Redis_ca5fc0f.php:312)
at Redis_ca5fc0f->get('DoctrineNamespaceCacheKey[]')
(vendor/doctrine/cache/lib/Doctrine/Common/Cache/RedisCache.php:47)
at Doctrine\Common\Cache\RedisCache->doFetch('DoctrineNamespaceCacheKey[]')
(vendor/doctrine/cache/lib/Doctrine/Common/Cache/CacheProvider.php:200)
at Doctrine\Common\Cache\CacheProvider->getNamespaceVersion()
(vendor/doctrine/cache/lib/Doctrine/Common/Cache/CacheProvider.php:177)
at Doctrine\Common\Cache\CacheProvider->getNamespacedId('9f7b27184e58ac025013b8c22fbdfa8a')
(vendor/doctrine/cache/lib/Doctrine/Common/Cache/CacheProvider.php:60)
at Doctrine\Common\Cache\CacheProvider->fetch('9f7b27184e58ac025013b8c22fbdfa8a')
(vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:271)
at Doctrine\ORM\Query->_parse()
(vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:295)
at Doctrine\ORM\Query->_doExecute()
(vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:967)
at Doctrine\ORM\AbstractQuery->executeIgnoreQueryCache(null, 1)
(vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:922)
at Doctrine\ORM\AbstractQuery->execute(null, 1)
(vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:726)
at Doctrine\ORM\AbstractQuery->getResult()
(src/Acme/AcmeBundle/Controller/AcmeController.php:1461)
at Acme\AcmeBundle\Controller\AcmeController->acmeAction(object(Request))
(vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:150)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:67)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:198)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/app_dev.php:35)
at runSymfony()
(public/app_dev.php:39)
at require('/var/www/html/project/Website/public/app_dev.php')
(vendor/symfony/symfony/src/Symfony/Bundle/WebServerBundle/Resources/router.php:42)
Please look into the problem
this might be helpful. Earlier my configurations used to be this:
snc_redis:
clients:
default:
type: predis
alias: default
dsn: "%redis_dsn%"
options:
read_write_timeout: "%redis_read_write_timeout%"
session:
type: phpredis
alias: session
dsn: "%redis_dsn%"
options:
read_write_timeout: "%redis_read_write_timeout%"
doctrine:
type: phpredis
alias: doctrine
dsn: "%redis_dsn%"
options:
read_write_timeout: "%redis_read_write_timeout%"
session:
client: session
prefix: session:main
doctrine:
metadata_cache:
client: doctrine
entity_manager: default # the name of your entity_manager connection
document_manager: default # the name of your document_manager connection
namespace: "%kernel.root_dir%"
result_cache:
client: doctrine
entity_manager: [default, read] # you may specify multiple entity_managers
query_cache:
client: doctrine
entity_manager: default
changed to the following
snc_redis:
clients:
default:
type: predis
alias: default
dsn: "%redis_dsn%"
options:
read_write_timeout: "%redis_read_write_timeout%"
session:
type: predis
alias: session
dsn: "%redis_dsn%"
options:
read_write_timeout: "%redis_read_write_timeout%"
doctrine:
type: predis
alias: doctrine
dsn: "%redis_dsn%"
options:
read_write_timeout: "%redis_read_write_timeout%"
session:
client: session
prefix: session:main
doctrine:
metadata_cache:
client: doctrine
entity_manager: default # the name of your entity_manager connection
document_manager: default # the name of your document_manager connection
namespace: "%kernel.root_dir%"
result_cache:
client: doctrine
entity_manager: [default, read] # you may specify multiple entity_managers
query_cache:
client: doctrine
entity_manager: default
Might help others.
I am integrating redis in my symfony2. I used this tuts:
http://olegpuzanov.com/2015/02/01/using-redis-for-doctrine-caching-in-symfony2/
ClassNotFoundException in appDevDebugProjectContainer.php line 966: Attempted to load class "Redis" from the global namespace.
Did you forget a "use" statement?
here are my configuration:
snc_redis:
# configure predis as client
clients:
default:
type: predis
alias: default
dsn: redis://localhost
doctrine:
type: predis
alias: doctrine
dsn: redis://localhost
# configure doctrine caching
doctrine:
metadata_cache:
client: doctrine
entity_manager: Content
document_manager: default
result_cache:
client: doctrine
entity_manager: [Content]
query_cache:
client: doctrine
entity_manager: Content
and my query:
$query = $this->createQueryBuilder('c')
->select('c.id, c.title, c.sequence, c.sequence_count, c.category_sequence, c.unique_id, c.priority, c.status, c.created_at,c.kaltura_id')
->addSelect('o.slug as owner')
->addSelect('cat.slug as category')
->addSelect("group_concat(m.name SEPARATOR ',') AS media")
->addSelect("group_concat(a.name SEPARATOR ',') AS album")
->innerJoin('c.content_owner', 'o')
->innerJoin('c.category', 'cat')
->leftJoin('c.media', 'm')
->leftJoin('c.albums', 'a')
->groupBy('c.id')
->setResultCacheDriver($predis)
# set cache lifetime
->setResultCacheLifetime($cache_lifetime)
->getQuery()
->getArrayResult();`
also i registered the snc bundle in my app kernel.
please help
git clone https://github.com/phpredis/phpredis.git
cd phpredis
git checkout php7
phpize
./configure
make && make install
cd ..
rm -rf phpredis
See https://gist.github.com/hollodotme/418e9b7c6ebc358e7fda
enter image description here
If first time you use redis, You must uncomment redis extension for php config and restart your server. So your problem solved
I am having difficulty configuring the Doctrine Extension Taggable provided here:
https://github.com/FabienPennequin/DoctrineExtensions-Taggable
My project is using Symfony 2 Fullstack and my configuration is using yaml while my doctrine entities are using annotation. I installed DoctrineExtensions using composer. Adding "fpn/doctrine-extensions-taggable": "dev-master" to the require section on composer.json and then running composer update. This installed without issue.
I then become lost at this section: https://github.com/FabienPennequin/DoctrineExtensions-Taggable#setup-doctrine
I understand that the metadata is a Doctrine Entity however as previously mentioned I am using yaml for my symfony configuration as well as entity managers. Here is the excerpt from my config.yml file:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
default_entity_manager: main
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
main:
connection: default
mappings:
VendorMainBundle:
prefix: Vendor\MainBundle\Entity
taggable:
connection: default
mappings:
taggable:
type: xml
prefix: DoctrineExtensions\Taggable\Entity
dir: %kernel.root_dir%/../vendor/fpn/doctrine-extensions-taggable/metadata
However, when I run php app/console doctrine:mapping:info --em=taggable I get the error:
[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors.
Should the above command show the mappings described in the xml files?
Thereby allowing me to update the schema in the database?
I used this documenation as reference for the config.yml file: http://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration
I also added this under the config.yml in order to setup the TagListener. Is this correct?
services:
taggable:
class: DoctrineExtensions\Taggable\TagListener
EDIT [#Grimv01k]:
The TagListener requires an argument passed that is an instance of the TagManager Object. I created another service to handle that as follows and passed it to the TagListener:
tag.manager:
class: DoctrineExtensions\Taggable\TagManager
tags:
- { name: doctrine.event_subscriber, connection: default }
arguments:
entity.manager: #doctrine.orm.entity_manager
taggable:
class: DoctrineExtensions\Taggable\TagListener
arguments:
manager: #tag.manager
The TagManager requires an argument of the entityManager however by doing so results in error:
[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]
Circular reference detected for service "doctrine.dbal.default_connection", path: "doctrine.dbal.default_connection".
Across the web it's recommended to resolve this error by passing #service_container and in the constructor of the object pull out the entity_manager, however being a vendor I'd prefer not to modify their code. Is there another way?
Just guess: maybe that happens becuse you haven't got tags applied in service, and Doctrine doesn't use this in your complier pass. Try to do it like this:
services:
taggable:
class: DoctrineExtensions\Taggable\TagListener
tags:
- { name: doctrine.event_subscriber, connection: default }
I start using Redis on me project (php-redis). Is a Symfony2 project and i found the:
https://github.com/snc/SncRedisBundle
I follow the installation process and i configured:
Some clients to store no-sql data and cache
Sessions storage
Doctrine metada, result and query cache
I create a new entity in a bundle and i fail because i create it at yml and i have all others with annotation system, so i delete yml format and create the annotation.
Every change i make on the annotation class (change the table name for example), is not affecting the schema or the database, even i recreate the database or try to execute cache:clear with all the options.
If i just comment the redis doctrine configuration lines, it works and i can see the changes on the schema.
Im maybe forgetting something, or i cant really find how to clean that doctrine redis cache.
¿I have to manually clean any position on the redis client use for caching?
Here is the configuration:
#Snc Redis Bundle
snc_redis:
clients:
d2a:
type: phpredis
alias: d2a
dsn: redis://localhost/1
cache:
type: phpredis
alias: cache
dsn: redis://localhost
logging: true
session:
client: d2a
prefix: redis_session
doctrine:
metadata_cache:
client: cache
entity_manager: default # the name of your entity_manager connection
document_manager: default # the name of your document_manager connection
result_cache:
client: cache
entity_manager: [default, read] # you may specify multiple entity_managers
query_cache:
client: cache
entity_manager: default
The easiest way but not the best one is to flush redis db with doctrine cache. Run
php app/console redis:flushdb --client=cache
(Not tested!) Another way is to setup doctrine metadata cache in doctrine config http://symfony.com/doc/current/reference/configuration/doctrine.html#caching-drivers
orm:
entity_managers:
# A collection of different named entity managers (e.g. some_em, another_em)
some_em:
metadata_cache_driver:
type: array # Required
host: ~
port: ~
instance_class: ~
class: ~