【问题标题】:Fos UserBundle with yaml - doctrine schema update带有 yaml 的 Fos UserBundle - 学说架构更新
【发布时间】:2013-05-21 18:42:01
【问题描述】:

我想使用FOSUserBundle,安装它并首先使用注释设置它,但因为我有其他实体我选择yaml。

我按照文档中的说明进行操作,创建了 yaml 文件并添加了一些我需要的字段。我生成了实体本身,然后在 php 文件中扩展了 BaseUser 类。

这部分工作正常,因为当我第一次想更新 SQL 时,它抛出了更改为受保护或公共的错误,但更新后,基本 FOS 字段不在数据库中。

这里是 User.orm.yml

Plastic\PublicBundle\Entity\User:
type:  entity
lifecycleCallbacks:
    prePersist: [setCreated, setModified]
    preUpdate: [setModified]
oneToMany:
    albums:
        targetEntity: Album
        mappedBy: user
    images:
        targetEntity: Image
        mappedBy: user
table: users
id:
    id:
        type: integer
        generator:
            strategy: AUTO
fields:
    firstName:
        type: string
        length: 50
    lastName:
        type: string
        length: 50
    gender:
        type: boolean
    dateOfBirth:
        type: date

与第一个相关部分的 User.php 实体:

<?php

namespace Plastic\PublicBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use FOS\UserBundle\Model\User as BaseUser;

/**
 * User
 */
class User extends BaseUser
{

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

/**
 * @var string
 */
private $firstName;

/**
 * @var string
 */
private $lastName;
    /**
 * Constructor
 */
public function __construct()
{
    parent::__construct();
    $this->albums = new \Doctrine\Common\Collections\ArrayCollection();
    $this->images = new \Doctrine\Common\Collections\ArrayCollection();
}

在我的 ORM config.yml 中,自动映射设置为 true。

【问题讨论】:

    标签: php symfony doctrine-orm yaml fosuserbundle


    【解决方案1】:

    代替

    use FOS\UserBundle\Model\User as BaseUser;
    

    尝试使用

    use FOS\UserBundle\Entity\User as BaseUser;
    

    也就是说,因为您使用的是 orm,所以您必须使用 Entity 基类,而不是 Model 基类。我也被这个问题困住了一段时间,但上面的改变让事情再次发生了。

    【讨论】:

    • 试过但没用,我认为现在 dev-master 分支没有区别,因为实体/文档也只是扩展模型,并且在 fosusrbundle 的描述中也说对于 sql,mongo 使用 Model 类一
    • 您使用的是 MongoDB 还是 ORM?因为您的代码表明您正在使用 ORM... 是的 Document 和 Entity 类都在扩展 Model。我认为这只是以不同的方式命名它们,以便库可以确定您正在使用哪种方法。
    • 我正在使用 ORM,但我尝试了您的答案,但没有成功。我设法让它像我在 yml 文件中创建所有内容一样工作,然后插入很好,但这不是最好的解决方案,只是一种解决方法。
    • FOS\UserBundle\Entity\User 在较新版本的 FOS 用户包中已弃用
    猜你喜欢
    • 2013-09-15
    • 2017-10-19
    • 1970-01-01
    • 2015-12-05
    • 2016-07-22
    • 2014-07-24
    • 2013-06-28
    • 2016-09-04
    • 2014-07-31
    相关资源
    最近更新 更多