php 找不到Symfony原则SQLSTATE 42S02基表或视图

mrfwxfqh  于 2023-03-11  发布在  PHP
关注(0)|答案(1)|浏览(165)

我在Symfony Bundle类中使用以下代码创建了一个带有Doctrine的Table:

<?php

namespace Acme\Bundle\TranslationMessagesProducerBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 *
 * @ORM\Entity
 * @ORM\Table(name="acme_translation_config")
 *
 */
class AcmeTranslationMessagesProducerEntity{
    
    /**
     * id - for doctrine
     * @ORM\Column(type="integer")
     * @ORM\Id()
     * @var integer
     */
    private $id;
    
    /**
     * enabled
     * @ORM\Column(type="boolean")
     * @var mixed
     */
    private $enabled;

    public function getId(){
        return $this->id;
    }
    public function getEnabled(){
        return $this->enabled;
    }

}

并且此表在运行php bin/console doctrine:schema:update --force后存在,我可以通过查询php bin/console doctrine:query:sql "Select * From acme_translation_config来验证它是否存在,也可以运行:php bin/console doctrine:query:sql "Select * From akeneo_pim.acme_translation_config
Doctrine也认识到这一点,并通过运行以下命令进行验证:php bin/console doctrine:mapping:info,结果为:

Found 58 mapped entities: .... 
Acme\Bundle\TranslationMessagesProducerBundle\Entity\AcmeTranslationMessagesProducerEntity

然而,如果我尝试从这个表中获取一个对象,如下所示:

$em = $this->getDoctrine()->getManager();
 $config = $em->getRepository(AcmeTranslationMessagesProducerEntity::class)->find(1);

我得到了失败:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'akeneo_pim.acme_translation_config' doesn't exist

怎么解决呢?

col17t5w

col17t5w1#

替换此

/**
 *
 * @ORM\Entity
 * @ORM\Table(name="acme_translation_config")
 *
 */

通过以下方式:

/**
 *
 * @ORM\Entity(repositoryClass="App\Repository\AcmeTranslationMessagesProducerEntityRepository")
 * @ORM\Table(name="acme_translation_config")
 *
 */

并检查AcmeTranslationMessagesProducerEntityRepository.php是否存在
PS:你应该遵守命名规则,不要在类名中添加“Entity”:AcmeTranslationMessagesProducer优于AcmeTranslationMessagesProducerEntity

相关问题