Hello, i need to explain what i'm was doing at first:
I was on a project and for some reason I ended up having to use two databases instead of just one. so i do what it was needed to do, i change my doctrine.yaml settings with what is explain in the symfony doc and in that moment the error appeared
An SQL error that says that it was no column found for X request on X table
(the error is SQLSTATE[42S02], but it's not important)
(in parallel of that error i need to say that the "--em" shit didn't work for me when i do php bin/console doctrine:migrations:diff/migrate --em=default or customer so i does indeed php bin/console doctrine:schema:update --force --em=default or customer and i don't know why ! so if you have something to say here i'll take it too.)
Anyway i go back to what i was explaining,
This sql error appear because of my controller, they working with default Repository implementation (MyClassNameRepository $myClasNameReposiotry for an exemple) that was normaly implement the default EntityManager but it doesn't and that throw this error.
At this point i though that i had an error in some files in symfony because of a shit i probably does. I don't have the time for replacing all the repository implementation, soo i create a new project with the doctrine config that is needed for working with two database according to the symfony doc. Is the same as the One in my project :
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
customer:
# configure these for your database server
url: '%env(resolve:DATABASE_CUSTOMER_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
entity_managers:
default:
connection: default
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
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
That is the same config than the one in symfony doc except for the Main dir.
i created my entities, one in each database and i does a crud on them.
The Hierarchy files:
src/
-----Entity/
----Product
----Customer/
-ProductCustomer
-----Repository/
----ProductRepository
----Customer/
-----ProductCustomerRepository
And same for the form
And with this testApplication i saw a new error, for the customer part i had the same sql error, because the column was searched in the wrong bdd, after changing the code for :
this
/**
* #Route("/", name="customer_produit_customer_index", methods={"GET"})
*/
public function index(ProductCustomerRepository $productCustomerRepository): Response
{
return $this->render('customer/produit_customer/index.html.twig', [
'produit_customers' => $productCustomerRepository->findAll(),
]);
}
to this
/**
* #Route("/", name="customer_produit_customer_index", methods={"GET"})
*/
public function index(): Response
{
$productCustomers = $this->getDoctrine()
->getRepository(ProduitCustomerRepository::class, "customer")
->findAll()
;
return $this->render('customer/produit_customer/index.html.twig', [
'produit_customers' => $productCustomers,
]);
}
The error has changed, as I expected and know i have :
The class 'App\Repository\Customer\ProduitCustomerRepository' was not found in the chain configured namespaces App\Entity\Customer
No matter how much I search the internet, I can't find anything that really solves my mistakes. Now I'm tired of looking, especially since I don't understand these errors because it's not the first time I've set up an application connected to two databases, but it's the first time that I have these errors.
So please help me =D.
Related
I'm trying to make migrations based on a diff of my entities.
I'm expecting for the initial run (and following runs) that doctrine generates a migration with the diff.
I've tried the following solution but this results in "No changes detected in your mapping information."
I've poked and went thru the doctrine migrations source code to see if I can extend/alter the behavior to no results.
I also found related issues in doctine dbal like this one and this one.
Is doctrine migrations still the way or is there another lib which is better suited for this problem?
I'm running PHP 7.4.4, Symfony 5.0.6, Doctrine Migrations 2.2.6 (latest stable), Postgres 12.1.
Doctrine config:
doctrine:
dbal:
driver: 'pdo_pgsql'
server_version: '12.1'
charset: utf8
url: '%env(resolve:DATABASE_URL)%'
dbname: storage001
default_dbname: storage001
Entity example:
/**
* #ORM\Table(schema="storage001", name="entityName")
*/
class EntityClass1
{
...
}
I am struggling to have symfony/doctrine exclude db views when validating and updating schema.
I first tried without doctrine migrations ( see this question) but that did not work.
I found out that doctrine migrations would filter out views from validation/update and it actually does, so that step seems to work with migrations.
So, if one has just one db doctrine migrations will work fine, but the set up with multiple db is not clean cut, to say the least.
This is a known issue as you can see on this link. Unfortunately when attempting to follow the solutions described in the link the results are messy.
Even though the command migrations:update --em=default will indicate the correct database is set up, when generating migrations:diff --em=default it will mingle with other db and likewise with migrations:migrate --em=default which ends up creating tables on the other db.
More specifically the errors are:
- it will create the separate directories for the migrations files as indicated in the config files, but not to the corresponding em
- it will generate mysql queries mixing up the two em
- consequently it will update db
My set up is as follows:
config.yml
imports:
....
- { resource: doctrine_migrations_default.yml }
- { resource: doctrine_migrations_used.yml }
doctrine:
dbal:
default_connection: default
connections:
default:
.....
#schema_filter: "~^(?!view).*$~"
schema_filter: ~^(?!view_)~
used:
......
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: default
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: default
auto_mapping: true
mappings:
AppBundle: ~
used:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: used
mappings:
UsedBundle: ~
Then, the specific configuration files for migrations are:
doctrine_migrations_default.yml
doctrine_migrations:
dir_name: "%kernel.root_dir%/DoctrineMigrationsDefault"
namespace: App\DoctrineMigrationsDefault
table_name: migration_versions
name: Application_Migrations_Default
doctrine_migrations_used.yml
doctrine_migrations:
dir_name: "%kernel.root_dir%/DoctrineMigrationsUsed"
namespace: Used\DoctrineMigrationsUsed
table_name: migration_versions
name: Application Migrations Used
organize_migrations: false
This is one example of how it mixes up the configs. The database name is correct. It corresponds to the em=default. But other info are coming from the em=used
Thanks
php bin/console doctrine:migrations:status --em=default
== Configuration
>> Name: Application Migrations Used
>> Database Driver: pdo_mysql
>> Database Name: symfony_cars
>> Configuration Source: manually configured
>> Version Table Name: migration_versions
>> Version Column Name: version
>> Migrations Namespace: Used\DoctrineMigrationsUsed
>> Migrations Directory: /Users/BAMAC/Sites/Symfony1/app/DoctrineMigrationsUsed
>> Previous Version: Already at first version
>> Current Version: 0
>> Next Version: 2017-10-19 08:03:52 (20171019080352)
>> Latest Version: 2017-10-19 08:03:52 (20171019080352)
>> Executed Migrations: 0
>> Executed Unavailable Migrations: 0
>> Available Migrations: 1
>> New Migrations: 1
Also, if I try to specifically indicate the configuration file with:
php bin/console doctrine:migrations:status --em=default --configuration=./app/config/doctrine_migrations_default.yml
It will not recognize the file info, even though it does so when it gets the info directly from config.yml. The following error is thrown.
[Doctrine\DBAL\Migrations\MigrationException]
Migrations configuration key "doctrine_migrations" does not exists.
If I take the doctrine_migrations key out it will generate an error on the next info it encounters.
Don't import the migrations settings in your config.yml file.
Your individual configuration files aren't actually configured correctly which is why you are receiving the error about the configuration keys not existing. The keys are different than they are in the normal migrations config. I had to search the code to find the right settings. (I found them around ln35 of the AbstractFileConfiguration.php file)
Try these -
doctrine_migrations_default.yml
migrations_directory: "app/DoctrineMigrationsDefault"
migrations_namespace: App\DoctrineMigrationsDefault
table_name: migration_versions
name: Application_Migrations_Default
doctrine_migrations_used.yml
migrations_directory: "app/DoctrineMigrationsUsed"
migrations_namespace: Used\DoctrineMigrationsUsed
table_name: migration_versions
name: Application Migrations Used
organize_migrations: false #valid entries are false, 'year', and 'year_and_month'
doctrine_migrations, dir_name, and namespace are not valid entries for that configuration file.
Also, you can't use %kernel.root_dir% in your directory path but what worked for me was either changing it to 'app' or providing a full path.
Leaving this for future references:
The files doctrine_migrations_default.yaml and doctrine_migrations_used.yaml are for extra configurations to use when performing migrations across multiple entity managers.
These files should not be autoloaded by symfony, you can put them under config/ directly, not in config/packages where the doctrine_migrations.yaml is located.
I tested it on symfony 5.0.4 and it is working perfectly:
php bin/console doctrine:migrations:migrate --em=used --configuration=config/doctrine_migrations_used.yaml
This is my orm config for my Doctrine in Symfony2
orm:
default_entity_manager: default
auto_generate_proxy_classes: false
entity_managers:
default:
connection: default
auto_mapping: false
mappings:
MyMappings:
type: yml
is_bundle: false
dir: %kernel.root_dir%/../src/Bundle/Entity/orm
prefix: Bundle\Entity
My directory structure is set up like this:
src
-- Bundle
--- Entity
---- orm
----- Bundle.Entity.(TableName).orm.yml
My question is why when I run:
php app/console doctrine:generate:entities MyMappings
I would get the following exception:
php app/console doctrine:generate:entities MyMappings
[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'Bundle.Entity.(TableName).orm.yml' for class 'Bundle\Entity\(TableName)'
the orm files were named (TableName).orm.yml
And the yml:
TableName:
type: entity
When I used the import from database I added the namespace:
php app/console doctrine:mapping:convert yml ./src/Bundle/Entity/orm --force --from-database --namespace="Bundle\\Entity\\"
which generated the Bundle.Entity.(TableName).orm.yml files.
And the yml:
Bundle\Entity\TableName:
type: entity
Now, I keep getting
[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'Bundle.Entity.Bundle.Entity.(TableName).orm.yml' for class 'Bundle\Entity\Bundle\Entity\(TableName)'
I feel like I am missing something obvious as to how to properly generate these entities in the correct namespace. If I am not clear please let me know and I will try to rephrase this issue.
More Information:
I am trying to generate these entities in a non-bundle namespace
After creating entity with:
php app/console doctrine:generate:entity
and while using:
php app/console doctrine:schema:update --force
I encountered:
No Metadata Classes to process.
Entity
namespace ISLab\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="menu_items")
*/
class MenuItem
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="parent", type="integer")
*/
private $parent;
// ...
}
I had a similar problem and took me a while until I find out how to clear out the cache. Clearing only doctrine's cache did not work for me. Use the command below to clear the entire environment (production one in this case)
php app/console cache:clear --env=prod
I had the same problem when I generate an Entity through the interactive generator from command line. When I first created I set the Configuration Format to PHP and that didn't work. So I deleted it and tried again but this time I selected format = annotation. Then it worked! Maybe this could solve someone's problem.
So entity manager didn't create metadata for your entity. Here are things I would check first:
Clear your cache
Make sure your AdminBundle is registered in AppKernel.
Make sure that entity manager's automapping is true. Or set it up correctly if you're using custom EM.
A bit offtopic:
I see you're making a menu system and I would strongly suggest you to check out Doctrine Extensions Tree which should allow you to query and edit items much more efficiently than with your structure.
I had the same problem when i update the database.
So i follow this solution and i tried to adapt in my case and it works.
I think that is a problem of automapping indeed also with auto_mapping setted true, symfony wasn't able to find the entities.
I hope that it can help.
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
AppBundle:
type: annotation
is_bundle: false
dir: %kernel.root_dir%/../src/AppBundle/Entity/
prefix: AppBundle\Entity
alias: AppBundle
for anyone on the same thing, these are few steps to go:
1. Clear the cache:
bin/console c:c --env=dev
bin/console c:c --env=prod
If you see any errors meanwhile, try to cover it by inspecting source of problem.
2. Inspect your configuration:
According to this sample directory structure:
Application
|
└- src
|
└- Acme
|
└- Bundle
|
└- BlogBundle
|
└- Model
|
└- Entity
You should have this configuration inside your app/config/config.yml file:
doctrine:
...
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
AcmeBlogBundle:
mapping: true
type: annotation
dir: Model/Entity
alias: Blog
prefix: Acme\Bundle\BlogBundle\Model\Entity
is_bundle: true
3. Update your database schema:
You can now update your database schema without any problems:
bin/console doctrine:schema:update
despite of this being the final step for most cases, you may still use --verbose to find any further possible problems.
bin/console doctrine:schema:update --verbose
Make sure your entities are placed in your bundle's Entity directory. In my case they were in Entities instead.
For me generating all the entities from the cmd did the work.
In my case, i solve the problem creating the Entity folder. For some reason it had been eliminated. I hope that it can help.
I've had the same issue. For me it was that my directory called 'Entity' was not in the 'AppBundle' but in the 'src' directory. Moving the 'Entity' directory into the 'AppBundle' directory fixed the problem for me!
I've a weird problem with propel.
My configuration in config.yml:
propel:
dbal:
driver: %database_driver%
user: %database_user%
password: %database_password%
dsn: %database_driver%:host=%database_host%;dbname=%database_name%;charset=UTF8
path: %kernel.root_dir%/../vendor/propel
phing_path: %kernel.root_dir%/../vendor/phing
I created my database with that command: php app/console propel:database:create and it worked very well but when I'm trying to create my first table with php app/console propel:model:build i've a fatal error telling: PHP Fatal error: Class 'Phing' not found.
I verified in the folder, everything is there. I guess that is an autoloading problem but I've followed the documentation at http://www.propelorm.org/cookbook/symfony2/working-with-symfony2.html and http://symfony.com/doc/master/book/propel.html
Any idea? Thanks a lot!
Which dependencies management system do you use?
This is an autoloading issue, and you should add the following definition:
$loader->registerPrefixes(array(
...
'Phing' => __DIR__.'/../path/to/phing/classes/phing',
));
where path/to/phing is the path where you installed Phing.