SQLSTATE [42S02]:未找到基表或视图:1146 表 'pdone.BaseUser' 不存在

Posted

技术标签:

【中文标题】SQLSTATE [42S02]:未找到基表或视图:1146 表 \'pdone.BaseUser\' 不存在【英文标题】:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pdone.BaseUser' doesn't existSQLSTATE [42S02]:未找到基表或视图:1146 表 'pdone.BaseUser' 不存在 【发布时间】:2015-10-05 06:45:48 【问题描述】:

我在 SonataUserBundle 中使用 FOSUserBundle 1.3.x-dev。我已经按照UserBundle installation 上的每一步进行安装和配置。这就是我在实体中所拥有的:

#src/Application/Sonata/UserBundle/Entity/User.php

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;

class User extends BaseUser

    /**
     * @var int
     */
    protected $id;

    /**
     * Get id.
     *
     * @return int $id
     */
    public function getId()
    
        return $this->id;
    


#src/Application/Sonata/UserBundle/Entity/Group.php

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseGroup as BaseGroup;
use Doctrine\ORM\Mapping as ORM;

class Group extends BaseGroup

    /**
     * @var int
     */
    protected $id;

    /**
     * Get id.
     *
     * @return int $id
     */
    public function getId()
    
        return $this->id;
    

这是配置的样子:

#FOSUserBundle
fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class:     Application\Sonata\UserBundle\Entity\User

    group:
        group_class:   Application\Sonata\UserBundle\Entity\Group
        group_manager: sonata.user.orm.group_manager

    service:
        user_manager: sonata.user.orm.user_manager

    from_email:
        address:        noreply@pdi.com
        sender_name:    Mail Service

doctrine:
    dbal:
        types:
            json: Sonata\Doctrine\Types\JsonType
        default_connection:   default
        connections:
            default:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8

                logging: true
                profiling: true
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        entity_managers:
          default:
            #I have tried here using mappings and without it - same issue
            mappings:
                FOSUserBundle: ~
                ApplicationSonataUserBundle: ~
                SonataUserBundle: ~
            auto_mapping: true
            filters:
                softdeleteable:
                  class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                  enabled: false

这是AppKernel.php

...
new Sonata\CoreBundle\SonataCoreBundle(),
new Sonata\BlockBundle\SonataBlockBundle(),
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),
new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
new Sonata\AdminBundle\SonataAdminBundle(),
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
...

这是我在composer.json 文件中的内容:

"require": 
    ...
    "friendsofsymfony/user-bundle": "1.3.x-dev",
    "sonata-project/admin-bundle": "~2.3",
    "sonata-project/user-bundle": "^2.2",
    "sonata-project/doctrine-orm-admin-bundle": "~2.3",
    "sonata-project/easy-extends-bundle": "^2.1",
    ...
    "leaseweb/memcache-bundle": "*"

我在app/Resources/FOSUserBundle/views/Security/login.html.twig 中打印错误如下:

 error|trans(, 'FOSUserBundle') 

每次我尝试登录时都会收到这样的消息:

执行 'SELECT t0.username AS username1, t0.username_canonical AS username_canonical2, t0.email AS email3, t0.email_canonical AS email_canonical4, t0.enabled AS enabled5, t0.salt AS salt6, t0.password AS 时发生异常 password7, t0.last_login AS last_login8, t0.locked AS locked9, t0.expired AS expired10, t0.expires_at AS expires_at11, t0.confirmation_token AS Confirm_token12, t0.password_requested_at AS password_requested_at13, t0.roles AS roles14, t0.credentials_expired AS credentials_expired15, t0.credentials_expire_at AS credentials_expire_at16, t0.created_at AS created_at17, t0.updated_at AS updated_at18, t0.date_of_birth AS date_of_birth19, t0.firstname AS firstname20, t0。 lastname AS lastname21, t0.website AS website22, t0.biography AS biography23, t0.gender AS gender24, t0.locale AS locale25, t0.timezone AS timezone26, t0.phone AS phone27, t0.facebook_uid AS facebook_uid28, t0.facebook_name AS facebook_name29, t0.facebook_data AS facebook_data30, t0.twitter_uid AS twitter_uid31, t0.twitter_name AS twitter_name32, t0.twitter_data AS twitter_data33, t0.gplus_uid AS gplus_uid34, t0.gplus_name AS gplus_name35, t0.gplus_data AS gplus_data36, t0.token AS token37, t0.two_step_code AS two_step_code38 FROM BaseUser t0 WHERE t0.username_canonical = ? LIMIT 1' with params ["admin"]: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pdone.BaseUser' 不存在

这是为什么呢?这是 FOSUserBundle 问题还是 SonataUserBundle 问题?有人可以给我一些建议吗?

编辑

我通过运行以下命令执行了另一项测试:

# php app/console doctrine:schema:update --dump-sql
Nothing to update - your database is already in sync with the current entity metadata.

# php app/console doctrine:schema:drop --force
Dropping database schema...
Database schema dropped successfully!

# php app/console doctrine:schema:create
ATTENTION: This operation should not be executed in a production environment.

Creating database schema...
Database schema created successfully!

同样没有成功,同样的问题!在所有测试中,我首先清除缓存

编辑 2

我运行了doctrine:mapping:info,得到了这个输出,为什么?

Found 18 mapped entities:
[OK]   FOS\UserBundle\Entity\User
[OK]   FOS\UserBundle\Entity\Group
[OK]   Sonata\UserBundle\Entity\BaseGroup
[OK]   Sonata\UserBundle\Entity\BaseUser
[OK]   Application\Sonata\UserBundle\Entity\User
[OK]   Application\Sonata\UserBundle\Entity\Group
[OK]   PDI\PDOneBundle\Entity\Representative
[OK]   PDI\PDOneBundle\Entity\Media
[OK]   PDI\PDOneBundle\Entity\Brand
[OK]   PDI\PDOneBundle\Entity\TerritoryBrand
[OK]   PDI\PDOneBundle\Entity\Email
[OK]   PDI\PDOneBundle\Entity\Company
[OK]   PDI\PDOneBundle\Entity\Action
[OK]   PDI\PDOneBundle\Entity\Message
[OK]   PDI\PDOneBundle\Entity\Territory
[OK]   PDI\PDOneBundle\Entity\TargetBrand
[OK]   PDI\PDOneBundle\Entity\Event
[OK]   PDI\PDOneBundle\Entity\Target

【问题讨论】:

【参考方案1】:

错误很明确,表BaseUser不存在。

未找到基表或视图:1146 表 'pdone.BaseUser' 不存在 存在

此外,请查看FOSUserBundle User entity 类。 缺少一些注释(@ORM\Entity@ORM\Table(name="..."))。

例如

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

    /**
     * @ORM\Entity
     * @ORM\Table(name="fos_user")
     */
    class User extends BaseUser
    

确保使用命令生成数据库表

app/console doctrine:schema:update --force

编辑:

如Sonata reference 中所述,您是否尝试在您的config.yml 中添加此代码?

# Enable Doctrine to map the provided entities
doctrine:
    orm:
        entity_managers:
            default:
                mappings:
                    FOSUserBundle: ~
                    ApplicationSonataUserBundle: ~
                    SonataUserBundle: ~

【讨论】:

这并不能解决我的问题,我按照你说的做了,问题仍然存在 @ReynierPM 我在命令app/console doctrine:schema:update --force 中修复了一个问题。不要忘记添加--force。你有同样的问题吗? 是的,除了添加 --force 之外,没有其他方法可以运行命令,是的,我添加了,没有相同的问题 这个我也试过了,同样的问题,我已经把这个加到OP中了【参考方案2】:

如果命令 php app/console doctrine:schema:update --dump-sql 没有显示任何内容,则您的实体未映射。

我的第一个猜测是您需要在 config.yml 中添加 auto_mapping 而不是 Sonata 配置文档,这样您的 Entity 将被自动映射。

doctrine:
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        entity_managers:
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: true

同时检查您的 yourapp/AppKernel.php 是否有 SonataUserBundle 扩展 FOSUserBundle :

new Sonata\UserBundle\SonataUserBundle('FOSUserBundle')

【讨论】:

通过运行控制台命令,我得到了这个Nothing to update - your database is already in sync with the current entity metadata.,是的,我在config.ymlauto_mapping:true,但同样的问题

以上是关于SQLSTATE [42S02]:未找到基表或视图:1146 表 'pdone.BaseUser' 不存在的主要内容,如果未能解决你的问题,请参考以下文章

SQLSTATE [42S02]:未找到基表或视图:1146 表 'pdone.BaseUser' 不存在

SQLSTATE [42S02]:未找到基表或视图:1146 表 'laravel_abonamenty2.currencies' 不存在

SQLSTATE [42S02]:未找到基表或视图:1146表'softwareproject.o_r_f_o_l_s'在laravel中不存在错误

未找到基表或视图:1146 表 Laravel 5

未找到基表或视图:1146 表“epharmacy.medicines”不存在

创建类别并重定向到 category.index 后,在 laravel 中找不到基表或视图