Symfony2 FosUserBundle和SonataUserBundle:重写实体?

时间:2012-09-27 08:59:59

标签: symfony doctrine-orm fosuserbundle symfony-2.1

我在我的Symfony2项目中使用FosUserBundle和SonataUserBundle。

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

这是我的配置:

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

在app / Application / Sonata / userBundle / Entity /

中添加了字段的User实体
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 app / console doctrine:schema:update --force”时,不会创建添加的字段(country,city ...)。如何向用户实体添加字段?我迷失在fosuserbundle和sonatauserbundle ....

2 个答案:

答案 0 :(得分:6)

事实上,我使用fos和sonata用户捆绑包的方式似乎迫使我使用XML定义。因此,例如添加一个名为“city”的字段,您必须添加以下行:

User.php(在/ app / Application / Sonata / UserBundle / Entity /中):

protected $city;

User.orm.xml(在/ app / Application / Sonata / UserBundle / Ressource / config / doctrine /中):

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

将这个放在捆绑包的文档中对新手来说很有意思;)

答案 1 :(得分:3)

如果您想使用注释,我找到了更好的解决方案。

删除UserBundle/Resorces/config/doctrine文件夹,您可以在UserBundle/Entity文件夹

中使用注释
相关问题