I have setup a SonataAdminBundle on my Symfony 2.1.2 project and it works correctly. Now I´m trying to setup a SonataMediaBundle but I get this error:
==> php app/console sonata:easy-extends:generate SonataMediaBundle
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "doctrine.connections".
I copied the config parameters to config.yml as indicates the documentation. You can see it there: http://pastebin.com/wys11net
Any help or clue?
Thanks in advance
Looks like you're missing the Connections node inside the Doctrine > DBAL's node, aswell the specification of a default connection (among multiple connections, if that would be the case).
An example of the right template would be like
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
I just ran into this problem as well, and found that the "connections" node is not actually required in the config (see http://symfony.com/doc/current/reference/configuration/doctrine.html#doctrine-dbal-configuration ... you only need the connections node when you have multiple connections).
In my case, it was that I had the SonataCacheBundle in the AppKernel.php file before Doctrine. Doctrine sets the "doctrine.connections" parameter when Doctrine initializes, so if you try to access it in SonataCacheBundle before Doctrine has initialized then "doctrine.connections" is not in the container yet.
Reordering the entries in AppKernel.php fixed the issue for me.
Related
I've a Symfony app on Heroku with ClearDb addons. I need to manage the app for test and prod. So I need two database: one for test and one for the production(principle);
I tryed the Heroku Pipeline, but when I promote the app from staging to production, the production app is connetted to staging db. How can solve ?
How you manage it?
EDIT
I discovered the mistake. I set the parameters via
$db = parse_url(getenv('CLEARDB_DATABASE_URL'));
$container->setParameter('database_host', $db['host']);
From a quick search for $container->setParameter I can see that this is a Symfony feature to interpolate values into code, however they mention the following warning in their docs:
NOTE: You can only set a parameter before the container is compiled:
not at run-time. To learn more about compiling the container see
Compiling the Container.
https://symfony.com/doc/current/service_container/parameters.html#getting-and-setting-container-parameters-in-php
Heroku handle only symfony apps in prod env. So the stage app also have the environment var as "prod". How can I set parameters for different env? Or dynamically?
Thanks,
AlterB
I've solved with the Environment Variables.
I changed into app/config/config.yml this:
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
with that
doctrine:
dbal:
driver: pdo_mysql
url: '%env(CLEARDB_DATABASE_URL)%'
The app gets the db connection directly from Heroku Env. And it's done!
I use doctrine ORM in a Symfony 2.8 project.
My project contains several Bundles. For one Bundle, which generates Reports I want to use the db server with the slave replication as not to stress the master db server.
How to set this up?
What I tried so far:
In the config.yml
Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
slave:
driver: pdo_mysql
host: '%database_host_slave%'
port: '%database_port_slave%'
dbname: '%database_name_slave%'
user: '%database_user_slave%'
password: '%database_password_slave%'
charset: UTF8
Here I created my second db connection with the values stored in my parameters.yml.
I seem to get the orm configuration I tried to setup in the same file not correctly.
Let my first explain what I need:
I have a "ReportingBundle" which runs a console command. The entity manager is only needed to provide the proper authorization to the needed db server. The queries itself are oure SQL and I don't use the entities.
my service.yml for this bundle:
services:
myproject.reporting.service.csv_report_attachment:
class: Myproject\ReportingBundle\Service\DefaultCsvReportAttachmentService
arguments: ['#doctrine.orm.slave_entity_manager', '#logger', '#myproject.reporting.service.php_template_engine', 'reportingHtmlMailTemplate.php']
Now my non functioning orm setup in the config.yml:
ORM configuration
orm:
auto_generate_proxy_classes: '%kernel.debug%'
# naming_strategy: doctrine.orm.naming_strategy.underscore
default_entity_manager: default
entity_managers:
slave:
connection: slave
mappings:
MyprojectReportingBundle: ~
default:
connection: default
auto_mapping : true
metadata_cache_driver: redis
query_cache_driver: redis
result_cache_driver: redis
This results in
[Doctrine\ORM\ORMException]
Unknown Entity namespace alias 'MyprojectReportingBundle'.
I tried to follow the documentation here:
http://symfony.com/doc/current/reference/configuration/doctrine.html#custom-mapping-entities-in-a-bundle
Question:
What is the correct syntax, so that my query is run on the slave server instead of the default server?
I believe you have everything configured correctly. However, you need to make sure the Entity classes are defined in the Bundle namespace. So for example you'd want to have all the entities defined within Myproject\ReportingBundle\Entity in your example.
I'm trying to set a different entity listener resolver because I want to use a couple of services among a few lifecycle callbacks. I googled out some answers that say it should be set in config the way similar to this one:
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
entity_listener_resolver: {HERE!}
However, when I do it this way, I get the following error:
Unrecognized options "entity_listener_resolver" under "doctrine.orm.entity_
managers.default"
Moreover, when I look at the configuration reference, I don't see such option anywhere or an option to set it: http://symfony.com/doc/current/reference/configuration/doctrine.html
So how do I change the entity listener resolver?
I'm using the newest Sf 2.5 version.
I'm running into the same issue, when using "doctrine/doctrine-bundle": "~1.2".
You have two options.
1) Use doctrine/doctrine-bundle 1.3 Beta, where the key entity_listener_resolver exists.
2) Or use the following gist to get it working without using the key: https://gist.github.com/vadim2404/9538227
More infos: http://egeloen.fr/2013/12/01/symfony2-doctrine2-entity-listener-as-serice/
Thx to the guy from the irc channel.
You can use it like this only in the services.yml for a specific bundle:
orm:
entity_listener_resolver: {your_entity_listener_resolver}
But in the config.yml file you need to set the entity listener resolver for a specific entity manager for example the default one:
orm:
entity_managers:
default:
entity_listener_resolver: {your_entity_listener_resolver}
I have an app that talks to two different databases. For one of my entities, School, I get the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pressbox.schools' doesn't exist
That makes sense because there is no pressbox.schools. It's fnt.schools. It's trying to talk to the wrong database.
How do I tell my entity which mapping I want it to use? I would of course rather refer to the mappings than to the database names themselves, which can be different depending on the environment.
First, declare your connections in a config file (config.yml would be fine):
doctrine:
dbal:
default_connection: pressbox # change it as you wish
connections:
pressbox:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: pressbox
user: pressbox_usr
password: pressbox_pwd
charset: UTF8
fnt:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: fnt
user: fnt_usr
password: fnt_pwd
charset: UTF8
Then declare the entity managers:
doctrine:
orm:
default_entity_manager: pressbox
entity_managers:
pressbox:
connection: web
fnt:
connection: fnt
Now, in a controller, you can tell Doctrine which entity manager to use by passing its name to getEntityManager():
$fntEm = $this->getDoctrine()->getEntityManager('fnt');
Assuming the entity manager for the fnt table is called that same name.
i think cross database relations are pretty complicated or you have to code by hand through some plugin.
i found this question about the same topic which might help you:
Multiple database connection in Doctrine2 and Zend framework
In a Symfony2 project, you can configure the databases connections at the app/config/parameters.ini file. Documentation states that you can use, among others, sqlite3 PDO driver.
But configuring sqlite doesn't works well:
[parameters]
database_driver = pdo_sqlite
database_host = localhost
database_port =
database_name = test_project.db
database_user = root
database_password =
Using app/console doctrine:database:create, successfully creates a test_project.db file at the project root directory.
But after creating some entities, then running app/console doctrine:schema:update --force should create the tables on the database file, but it doesn't, file appears empty, with O bytes size.
Note that using any other PDO driver works well, but not with SQLite...
I've also tried to use the full path for the db file in the database_name parameter, but to no avail, database still doesn't gets updated.
For reference, here's the doctrine dbal section of the config.yml file:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
Is there a way around this? configurations missing? something not stated on the official doc of symfony2 project?
According to Doctrine the elements used for sqlite DBAL configuration are:
user (string): Username to use when connecting to the database.
password (string): Password to use when connecting to the database.
path (string): The filesystem path to the database file. Mutually exclusive with memory. path takes precedence.
memory (boolean): True if the SQLite database should be in-memory (non-persistent). Mutually exclusive with path. path takes precedence.
This is also listed in the full reference for Doctrine configuration in Symfony2, although not elaborated on.
So you need to switch up your config params to match whats appropriate for sqlite.
Here is what I needed to get SQLite to work, just after doing symfony new myapp :
in app/config.yml :
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_sqlite
path: "%database_path%"
In app/config/parameters.yml:
parameters:
database_path: "%kernel.root_dir%/db/myapp_%kernel.environment%.db3"
...
Next I could do a composer install, create a new entity and it just worked.
I've found that if I add a path line pointing at the database_name to my config.yml, sqlite seems to pick that up, and MySQL doesn't seem to complain.
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
path: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
This means you can still keep all database information in the parameters file, you don't need separate configs depending on which database you are using.
Mainly the file path or the file path permisssion will have issue.
In config.yml, set path to full path like
/home/{name}/NB/PHP/Symfony/test/src/Database/data.db3
Dont give %database_path% or what ever. Try this it will work.
If it works you can give as
%kernel.root_dir%/../src/Database/%database_path%
Also check sqlite is ok by
phpinfo(INFO_MODULES);
In view/output you can see pdo_sqlite and its version.
In my case setting a username and password in config/packages/doctrine.yaml did not create a username/password protected sqlite database.
doctrine:
dbal:
charset: UTF8
url: '%DATABASE_URL%'
user: 'foo'
password: 'bar'
It seems like the parameters username and password are ignored?