Im running symfony with xampp. Trying to connect to and create db trhough doctrine framework. When i write doctrine:database:create im answered with Could not find driver.
But my pdo driver works well. It is present in phpinfo, plus i can easily connect to db through the pdo constructor.
Here is my doctrine configs:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
driver: pdo_mysql
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
And my env:
DATABASE_URL="mysql://root:#127.0.0.1:3306/aihara"
check if you don't have another php.ini where the pdo_mysql is disabled
Related
I'm tring to use postgres whi symfony 5 and php 7 on windows 10 .
in my env file :
DATABASE_URL="postgresql://postgres:1234#127.0.0.1:5432/dbtest?serverVersion=12&charset=utf8"
I get could not find driver .
currelty in my php.ini file :
...
extension=pdo_pgsql
extension=pdo_sqlite
extension=pgsql
so i tried this database url
DATABASE_URL="pdo_pgsql://postgres:1234#127.0.0.1:5432/dbtest?serverVersion=12&charset=utf8"
doctrine.yaml conf :
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Ap
after : php bin/console d:d:c
i get :
i don't understand this behavior
The database url is taken as a database name in phpmyadmin
The database should be a postgres database so why i see the dabase under phpmyadin ?
I will be really gratful if some explain this to me !
I used this documentation : https://symfony.com/doc/4.4/doctrine/multiple_entity_managers.html
So now I can create new databases named legacy and Project like this
doctrince.yaml:
doctrine:
dbal:
default_connection: legacy
connections:
legacy:
driver: pdo_mysql
host: "%env(database_host)%"
port: "%env(database_port)%"
dbname: "%env(database_name)%"
user: "%env(database_user)%"
password: "%env(database_password)%"
charset: UTF8
project:
driver: pdo_mysql
host: "%env(database_host_project)%"
port: "%env(database_port_project)%"
dbname: "%env(database_name_project)%"
user: "%env(database_user_project)%"
password: "%env(database_password_project)%"
charset: UTF8
orm:
default_entity_manager: legacy
entity_managers:
legacy:
connection: legacy
mappings:
Legacy:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/..."
prefix: '...'
alias: Legacy
project:
connection: project
auto_mapping: true
mappings:
Project:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity"
prefix: 'App\Entity'
alias: Project
Now I have several Fixture classes all of them depend on each other and also some will create fixture for legacy and some will create for project.
Now my question is when I do:
php bin/console doctrine:fixtures:load --em=legacy
It runs the appfixtures of the project but not the of the bundle.
and then i get this error:
The class .. was not found in the chain configured namespaces ....
My question is now how can I load fixtures on multiple databases with multiple connections.
Thanks in advance.
You should read this doc : https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
I think it may solve your problem as this is exactly what you trying to do.
If you have to create a fixture for legacy just use :
$entityManager = $this->getDoctrine()->getManager('legacy');`
else if it is for project use
$entityManager = $this->getDoctrine()->getManager('project');`
Also, if you change doctrine.dbal.default_connection, entityManager will run against the defined connection.
So you can try to create group of fixtures.
And then run fixtures for project :
Edit config for test environment
#config/packages/test/doctrine.yaml
doctrine:
dbal:
default_connection: 'project'
Then run fixture for 'project' group
> php bin/console doctrine:fixtures:load --group=project
Then do the same thing for 'legacy'
I've been trying to learn how to use a bundle in symfony, but ran into some problems making the bundle's entities appear in my database. I finally got it to work by adding the last couple lines to my doctrine.yaml file. However for some reason it seems unlikely that this is the correct way to go about it, giving that it only works if I set the 'is_bundle' property to false, while it's probably supposed to be true, given that it is an entity from a bundle.
What is the correct way to make this entity appear in my database?
My doctrine file:
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
unix_socket: '/Applications/MAMP/tmp/mysql/mysql.sock'
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
default:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: default
ch_cookie_consent_bundle:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/vendor/connectholland/cookie-consent-bundle/Entity'
prefix: 'ConnectHolland\CookieConsentBundle\Entity'
alias: ch_cookie_consent_bundle
The bundle itself can be found here: https://github.com/ConnectHolland/cookie-consent-bundle
The is_bundle flag instructs the doctrine extension to match the configuration key to the bundle name.
In your case the configuration should be
orm:
mappings:
default:
...
CHCookieConsentBundle: # This should match the bundle name (in %kernel.bundles%)
is_bundle: true
alias: ch_cookie_consent_bundle
All the others configuration keys are optional and auto-detected.
We have a Main database with multiple tables. we are going to have an unknown amount of 'duplicate' databases depending on the client currently being used.
this was setup like the following
doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
server_version: '5.7'
charset: utf8mb4
url: '%env(resolve:DATABASE_URL)%'
School_A:
driver: pdo_mysql
server_version: '5.7'
charset: utf8mb4
url: 'mysql://nibbr:nibbr#127.0.0.1:3306/School_A'
School_B:
driver: pdo_mysql
server_version: '5.7'
charset: utf8mb4
url: 'mysql://nibbr:nibbr#127.0.0.1:3306/School_B'
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
4bf40159870dc1b23c97e7906a303f39:
connection: School_A
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
46f0618dadff645591073709906f006c:
connection: School_B
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src'
prefix: 'App'
now the Databases exist,
migrations using
php bin/console doctrine:migrations:migrate --em=46f0618dadff645591073709906f006c
worked perfectly fine.
but i cannot switch to specific EntityManagers to manipulate the correct database. it always uses the Default.
I know this has been touched on alot but no real answers have been given
each database uses all the same Entities and repositories. all i want to change is which database symfony 4 uses.
$rep = $this->getDoctrine()->getRepository(SchoolStudent::class,'46f0618dadff645591073709906f006c');
when i persist a new entry though $rep, no matter what value gets passed it only uses the first entity manager and connection it comes across in the doctrine.yaml. unless i pass a string that isnt an EntityManager name, then i get a 500 error.
using ANY variation of ->getManager() causes 500 error too. im fairly new to symfony
thank you kindly
I use two different managers that way (in services with dependency injection):
In this example i inject the repository directly into the service constructor.
<service id="my_service_id" class="FQCN">
<argument type="service">
<service class="Doctrine\ORM\DocumentRepository">
<factory service="doctrine.orm.46f0618dadff645591073709906f006c_entity_manager" method="getRepository"/>
<argument>Namespace\SchoolStudent</argument>
</service>
</argument>
</service>
I just started out using symfony. I installed SF4 with twig, doctrine maker, ...
I created my database 'Autotrader', I created a migration and migrated it.
But whenever I try to get the data from the database I get the following error:
This is the code I'm using:
<?php
namespace App\Controller;
use App\Entity\Car;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class CarController extends Controller
{
public function index()
{
$repository = $this->getDoctrine()->getRepository('App:Car');
$cars = $repository->findAll();
return $this->render('Car/list.html.twig', array(
"title" => "Our cars",
'cars' => $cars
));
}
}
Parameter config:
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''
doctrine:
dbal:
# configure these for your database server
driver: 'mysql'
server_version: '5.7'
charset: 'utf8mb4'
dbname: 'autotrader'
host: '127.0.0.1'
port: 3306
user: 'root'
password: 'secret'
# With Symfony 3.3, remove the `resolve:` prefix
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
This is the unknown database error.
Are you sure you have created Autotrader db at the correct localhost path and the port number mentioned in doctrine.yml file?
Also, In your doctrine.yml replace
dbname: 'autotrader' with dbname: Autotrader
and try again!!
I figured out what the problem was.
I was running my app in vagrant/homestead and migrated localy, no wonder the DB wasn't found