首页 > 解决方案 > Symfony SQLSTATE [42S22]:找不到列

问题描述

我有一个我不明白的错误,几天前我创建了一个新项目,用于尝试在文件较少的项目中解决此错误并解决了它,但该解决方案在我的 Real 项目中不起作用。(这个错误与我几天前问的另一个问题有关:在链配置的命名空间中找不到该类,但就像我说的我解决了它,所以也许我需要问一个新问题)。

我试图在我的项目中为多个数据库连接实现两个 EntityManager ,这就是问题所在,我的一个 entityManager 在错误的地方搜索了表,但没有找到主题...... IDK 为什么和最疯狂的事情是它的一些功能工作和其他没有,你会看到:

首先是我的教义.yaml 文件:

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

并且知道我可以在我的控制器中使用的一些功能的例子:

        // 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()
        ;

但是这个狗屎工作并返回一个包含所有列和值的数组:

        $connexion = $this->getDoctrine()->getConnection('default')
        $agentsFetch = $connexion->fetchAll("SELECT * from agent");

但是我不能使用 fetch 方法,因为如果我在我的模板中这样做,我需要将所有agent.firstName替换为agent.first_name例如,此外,我需要了解为什么一种方法有效而另一种方法无效......

最后,完整的错误是:

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

谢谢您的帮助 !

标签: phpsqlsymfonydoctrine

解决方案


好的

感谢Nicolai,他帮我找到了真正的问题,这是因为命名策略

我不知道为什么,但如果它没有在上下文中明确定义,使用几个 entityManager 学说将在数据库中生成camelCase中的表,而不是snake_case(下划线)。无论如何,这就是导致我问题的原因

所以必须用这一行指定:

naming_strategy: doctrine.orm.naming_strategy.underscore

对于我发现指定“下划线”的位置:

如何在 Doctrine 2 中配置命名策略

和文档对于那些想要的人:

https://symfony.com/doc/current/bundles/DoctrineBundle/configuration.html#configuration-overview


推荐阅读