[Symfony][Doctrine]File mapping drivers must have a valid directory path - php

I have a problems with Doctrine on Sf2.7, when I run a command such as "doctrine: schema: update" I get:
[Doctrine\Common\Persistence\Mapping\MappingException] File mapping drivers must have a valid directory path, however the given path 0 seems to be incorrect!
This worked well until I make a drop of a database to recreate it in "clean" after a lot changed entities ...
Here is an excerpt from my config.yml
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
dbname: "%database_name%"
host: "%database_host%"
port: "%database_port%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
auto_mapping: true
all entities begin with this type of annotations:
/**
 * User
 *
 * #ORM\Entity
 * #ORM\Table(name="usr_user")
 * #ORM\Entity(repositoryClass="Acme\UserBundle\Repository\UserRepository")
 */
I searched our friend Google, OpenClassRooms and here of course but not found ... someone an idea ?

Got the same error when I imported my project to another pc. The solution worked for me is to try clearing configuration cache using php artisan command.
php artisan config:cache
then try to create or update doctrine schema.

For Symfony, you need to clear var/cache directory.
It solved problem for me.

Related

Create Entity Manager in Symfony3?

I have an existing entity in a Symfony3 project that I am trying to add an entity manager to. My doctrine.yml file looks like this
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
lego:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
auto_mapping: true
default:
connection: default
mappings:
lego:
connection: lego
mappings:
AppBundle: ~
However anytime I try to access this entity manager through php bin/console doctrine:database:create --connection=lego
, it says the manager does not exist! I'm not sure if I'm missing a step - the only thing I've done to create the manager is to make the yml file.
It doesn't show up when I run bin/console debug:container.
Grateful for any help!
1) You are using the same schema name (database name) on the same host in two different entity managers without schema_filters. This means that when you do an update to one of the entity managers it will attempt to delete the data of the other entity manager.
2) You are asking about a entity manager but in the command you are passing a connection.
3) Run php bin/console debug:container | grep doctrine
This will give you all the doctrine services. Update your question accordingly.
4) Provide the exact command and exact error you get so we can track it down to the origin
5) For a more information run the command in verbose mode adding -v at the end
6) Are you importing your doctrine.yml in your config.yml ?

Multiple connections and entity managers in a symfony 2 / 3 application

My multi-tenant-app uses a master database, that holds information about tenants (like name, etc.) and a app-specific database per tenant.
I configured a master and some_tenant connection and entity manager in the doctrine section inside config.yml.
This gives me access to the master database from a controller (eg. for validating and getting tenant information for some_tenant based on the subdomain some_tenant.my-app.com). And it lets me use a tenant-specific database and entity manager during the application life-cycle.
The doctrine section in my config looks like this:
doctrine:
dbal:
default_connection: 'master'
connections:
master:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
some_tenant:
driver: pdo_mysql
host: "%database_host_some_tenant%"
port: "%database_port_some_tenant%"
dbname: "%database_name_some_tenant%"
user: "%database_user_some_tenant%"
password: "%database_password_some_tenant%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
master:
connection: master
mappings:
BEMultiTenancyBundle: ~
some_tenant:
connection: some_tenant
mappings:
AppBundle: ~
Here comes the part, which I am unhappy with and cannot find a solution:
First of all, tenants will be more than 20. And it starts to get messy, altering the doctrine config this way.
Then there is another config file, called tenants.yml which holds more information like enabled/disabled app-features, tenant-specific themes, etc.
The file is loaded, validated using the Config Component, and a container parameter is set, so that tenants configurations are available app-wide.
I would like to store the database credentials also in that file.
I want to create connections and entity managers based on that config. One per each tenant, which can be used during the app life-cycle.
I need to have them available also at the console command bin/console doctrine:schema:update --em=some_tenant for updating each tenant's database schem and bin/console doctrine:schema:update --em=master for updating the master database scheme.
By now I guess, the only way to achieve this is to add configuration parameters to the doctrine section programmatically after the AppBundle is loaded, and before the doctrine registry is constructed with the given managers and connections.
But I cannot even find a point, where I could achive this.
Is there another way to get to point 1 and 2?
Even though there is a similiar question, which I am not sure if it is exactly about the same problem and is about 3 years old, I wanted to post this question with a bit more explanation.
The solution in comments is a straight way easy solution and it will work. The other solution I've ever meet is to dynamically replace tenant database credentails using some external conditions, i.e request HOST.
You can decorate or extend the connection factory with a service in a way, that having the request stack available (or providing the domain with ENV or console argument for other SAPI's) you can have the same entity manager (even default!) being configured on demand.
on a brief look this would look like
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Symfony\Component\HttpFoundation\RequestStack;
class DynamicConnectionFactory extends Factory
{
/** #var RequestStack */
private $requestStack;
public function __construct(array $types, RequestStack $stack)
{
parent::__construct($types);
$this->requestStack = $stack;
}
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array())
{
$host = $this->requestStack->getMasterRequest()->getHost();
$params = $this->replaceParamsForHost(array $params, $host);
return parent::createConnection($params, $config, $eventManager, $mappingTypes);
}
private function replaceParamsForHost(array $params, $host)
{
//do your magic, i.e parse config or call Memcache service or count the stars
return array_replace($params, ['database' => $host]);
}
}
I think that, To manage multi-tenant with symfony 2/3.
We can config auto_mapping: false for ORM of doctrine.
file: config.yml
doctrine:
dbal:
default_connection: master
connections:
master:
driver: pdo_mysql
host: '%master_database_host%'
port: '%master_database_port%'
dbname: '%master_database_name%'
user: '%master_database_user%'
password: '%master_database_password%'
charset: UTF8
tenant:
driver: pdo_mysql
host: '%tenant_database_host%'
port: '%tenant_database_port%'
dbname: '%tenant_database_name%'
user: '%tenant_database_user%'
password: '%tenant_database_password%'
charset: UTF8
orm:
default_entity_manager: master
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
master:
connection: master
auto_mapping: false
mappings:
AppBundle:
type: yml
dir: Resources/master/config/doctrine
tenant:
connection: tenant
auto_mapping: false
mappings:
AppBundle:
type: yml
dir: Resources/tenant/config/doctrine
After that, we cannot handle connection of each tenant by override connection info in request_listener like article: http://mohdhallal.github.io/blog/2014/09/12/handling-multiple-entity-managers-in-doctrine-the-smart-way/
I hope that, this practice can help someone working with multi-tenant
Regards,
Vuong Nguyen

Symfony 3: Can't make relations work

I have Entity(s) and EntityHtml(s) entities which has one-to-one relashionship (Entity stores metadata and EntityHtml acts like a cache, storing ready HTML chunks for rendering).
I have defined a relationship in Entity class:
/**
* #ORM\OneToOne(targetEntity="EntityHtml")
* #ORM\JoinColumn(name="entityId", referencedColumnName="entityId")
*/
private $entityHtml;
but it isn't working. Also I have a kind of feeling, that annotations don't work at all, because changing them has no effect upon workability of the application.
On the other hand, messing with .orm.xml(s) reflects in how application works.
Can I tell Symfony to update ORM XMLs based on changes to annotations?
Should I duplicate relation meta to XML?
Does Symfony use info at both XML and annotations or does it choose one source?
config.yml is default one:
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
Does it make any difference if you try this:
/**
* #ORM\OneToOne(targetEntity="EntityHtml")
* #ORM\JoinColumn(name="entity_Id", referencedColumnName="entityId")
*/
I'm thinking that "entityId" is the Id in EntityHtml, and you need to specify a different "name" value in JoinColumn. I think I ran into that problem.
Try it - I'm not sure if it will work.
Figured this one out. It's a configuration issue. In order to make annotations work (xml is default option), you have to explicitly configure it:
# Doctrine Configuration
doctrine:
...
orm:
...
mappings:
AppBundle:
type: annotation
Unfortunatelly that's not specified in tutorials.

Database does not have any mapping information Symfony2 Doctrine

NOTE: Its not a Duplicate issue cause I have tried everything I get on google but nothing have helped us.
I am trying to import Tables using Doctrine Reverse Engineering tool, but I m getting this message:
Database does not have any mapping information.
My Connection Details in Config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: null
dbname: pixel_ashish
user: root
password: abc123
charset: UTF8
schema_filter: ~^(?!some_table1|some_table2)~
orm:
default_entity_manager: default
auto_generate_proxy_classes: true
proxy_dir: "%kernel.cache_dir%/doctrine/orm/Proxies"
proxy_namespace: Proxies
resolve_target_entities: []
What I have tried So Far:
Running php app/console doctrine:mapping:import --force AcmeBlogBundle xml
gives same error
Tried to convert mapping also which does not make any sense cause mappings are not there but still tried didn't worked out.
Created a new project and tried above given configuration didn't worked.
Now I am out of ideas please help me to solve this.
You need to create some mapping information in your config.
You can do it by passing auto_mapping: true under orm section:
doctrine:
orm:
auto_mapping: true
or by manually defining it under orm section:
doctrine:
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
YourBundleName:
type: "xml"
dir: "Entity"
prefix: "Your\BundleName\Entity"

Symfony2 + Propel: no connection information

I'm trying to connect propel and symfony2 together, but the only thing that I get is this exception:
No connection information in your runtime configuration file for datasource [symfony]
I used the propel bundle which I added to the composer, and I edited the kernel to include the bundle.
My config.yml propel conf looks like that:
propel:
dbal:
driver: mysql
user: root
password: null
dsn: mysql:host=localhost;dbname=symfony;charset=UTF8
options: {}
attributes: {}
What can I be missing? I tried googling, but nothing really solves the issue.
Okay, I solved the issue.
The problem lied in the config.yml file, where the propel dbal data should be stored. I initially put it the way they describe it in the manual, like that:
propel:
dbal:
driver: "%database_driver%"
user: "%database_user%"
password: "%database_password%"
dsn: "%database_driver%:host=%database_host%;dbname=%database_name%;charset=%database_charset%"
According to the manual on both the propel and symfony's sites, it is enough. However, this doesn't seem to be the case for some reason yet unknown to me. What I had to do was to explicitly state the connection and name it precisely - symfony, like that:
propel
dbal:
default_connection: symfony
connections:
symfony:
driver: %database_driver%
user: %database_user%
password: %database_password%
dsn: %database_driver%:host=%database_host%;dbname=%database_name%;charset=UTF8
This way it worked. Sucks to be a junior developer sometimes.

Categories