php - Symfony SQLSTATE[42S22]: Column not found
Get the solution ↓↓↓I have an error that i don't understand, few days ago i create a new project for trying resolving this error in a project with fewer files and i solve it, but the solution didn't work in my Real project. ( This error is related to an other question i asked few day ago : The class was not found in the chain configured namespaces, but like i said i solve it so maybe i need to ask a new question ).
I tried to implement two EntityManager in my project for multple database connexion, and that is the problem, oneof my entityManager searched tables in the wrong place, and don't find theme ... IDK why and most crazy thing it's some function work and other didn't, you'll see that :
In first my doctrine.yaml file :
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:
default_entity_manager: default
entity_managers:
default:
mappings:
Tdsmo:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Tdsmo'
prefix: 'App\Entity\Tdsmo'
alias: Tdsmo
customer:
connection: customer
mappings:
Customer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Customer'
prefix: 'App\Entity\Customer'
alias: Customer
And know an exemple of some function that i can use in my controller :
// this one work, but his not the default connexion ..
// return all agent in agentCustomer that is in the second database.
$agentCustomers = $this->getDoctrine()
->getRepository(AgentCustomer::class, "customer")
->findAll()
;
// And this one, that is the default connexion, didn't work like this same for 'AgentRepository $agentRepository' that can be implemented in the method and called after for ->findAll()
// return an error
$agents = $this->getDoctrine()
->getRepository(Agent::class, "default")
->findAll()
;
BUT This shit work and return a array with all column and values :
$connexion = $this->getDoctrine()->getConnection('default')
$agentsFetch = $connexion->fetchAll("SELECT * from agent");
But i can't use the fetch method 'cause if i do in my template i need to replace all agent.firstName by agent.first_name for example, Furthermore i need to understand why one method work but not the other ...
Finally, the full error is :
An exception occurred while executing 'SELECT t0.id AS id_1, t0.email AS email_2, t0.roles AS roles_3, t0.password AS password_4, t0.firstName AS firstName_5, t0.lastName AS lastName_6, t0.registeredAt AS registeredAt_7, t0.telephone AS telephone_8, t0.fonction_id AS fonction_id_9, t0.subdivision_id AS subdivision_id_10, t0.section_id AS section_id_11 FROM Agent t0':
SQLSTATE[42S22]: Column not found: 1054 Champ 't0.firstName' inconnu dans field list
Thank you for your help !
Answer
Solution:
OKAY
Thanks to Nicolai, he help me found the real problem, it was because of the naming strategy !
I don't know why but if it is not explicitly defined in the context of the use of several entityManager doctrine will generate the tables in the database in camelCase, instead of the snake_case (underscore).In any case that's what was causing me problem
So it must be specified with this line :
naming_strategy: doctrine.orm.naming_strategy.underscore
For where i found to specify 'underscore' :
How to configure naming strategy in Doctrine 2
And the doc For thoses who want :
https://symfony.com/doc/current/bundles/DoctrineBundle/configuration.html#configuration-overview
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: you must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.