-->

Symfony2的FosUserBundle和SonataUserBundle:覆盖实体?(Symf

2019-07-31 23:33发布

我用FosUserBundle和SonataUserBundle我的Symfony2项目。

我现在感到困惑。 我想补充的字段为实体用户,但它不工作。 没有为架构,例如没有更新。

下面是我的配置:

AppKernel:
...
new FOS\UserBundle\FOSUserBundle(),
new Sonata\UserBundle\SonataUserBundle(),
new Application\Sonata\UserBundle\ApplicationSonataUserBundle('FOSUserBundle')


config.yml:
...
# FOSUserBundle Configuration
fos_user:
    db_driver:     orm                        # BDD type
    firewall_name: main                       # firewall name
    user_class:    Application\Sonata\UserBundle\Entity\User # entity class defined

并且用户实体添加字段,在应用程序/应用/索纳塔/ userBundle /实体/

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

/**
 * This file has been generated by the Sonata EasyExtends bundle ( http://sonata-    project.org/easy-extends )
 *
 * References :
 *   working with object : http://www.doctrine-project.org/projects/orm/2.0        /docs/reference/working-with-objects/en
 *
 * @author <yourname> <youremail>
 */
class User extends BaseUser
{

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

    /**
     * @var string
     */
    protected $institution;

    /**
     * @var string
     */
    protected $department;

    /**
     * @var string
     */
    protected $city;

    /**
     * @var string
     */
    protected $country;

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getInstitution()
    {
        return $this->institution;
    }

    /**
     * @return string
     */
    public function getDepartment()
    {
        return $this->department;
    }

    /**
     * @return string
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * @return string
     */
    public function getCountry()
    {
        return $this->country;
    }
}

在我的数据库,表fos_user_user似乎与用户保存的数据表。

打电话时不创建添加字段(国家,城市...)“的PHP应用程序/控制台学说:架构:更新--force”。 如何字段添加到用户实体? 我失去了在fosuserbundle和sonatauserbundle ....

Answer 1:

事实上,它似乎我使用fos和奏鸣曲用户捆绑的方式强迫我用XML定义。 因此,例如添加字段名为“城市”,你必须添加这些行:

user.php的(在/ app /应用/索纳塔/ UserBundle /实体/):

protected $city;

User.orm.xml(在/ app /应用/索纳塔/ UserBundle /的ressource /配置/教义/):

<field name="city" column="city" type="text"/>

也许把这个捆的文档将是新手有趣;)



Answer 2:

我发现了一个更好的解决方案,如果你想使用的标注。

删除UserBundle/Resorces/config/doctrine文件夹,你可以在你使用注释UserBundle/Entity文件夹



文章来源: Symfony2 FosUserBundle and SonataUserBundle : overriding entities?