简体   繁体   English

奏鸣曲用户捆绑包,覆盖

[英]sonata user bundle, override

I'm trying to use sonata user bundle. 我正在尝试使用奏鸣曲用户捆绑包。 I have to add some fields. 我必须添加一些字段。

I have seen a folder Application\\Sonata\\UserBundle which is created with the sonata user bundle installation. 我已经看到了使用Sonata用户捆绑包安装创建的文件夹Application \\ Sonata \\ UserBundle。

I have then tried to modify the Entity\\User.php file: 然后,我尝试修改Entity \\ User.php文件:

<?php

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

class User extends BaseUser
{
    /**
     * @var integer $id
     */
    protected $id;

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

For example adding: 例如添加:

<?php

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

class User extends BaseUser
{
    /**
     * @var integer $id
     */
    protected $id;

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * @var String $nom
     */
    protected $nom;
/**
 * Get nom
 *
 * @return string 
 */
public function getNom() {
    return $this->nom;
}

But my database is not updated and nothing work. 但是我的数据库没有更新,因此无法正常工作。 Do you knows how to do that? 你知道怎么做吗?

Alternatively I have also tried to write in the UserAdmin.php file: 另外,我也尝试写在UserAdmin.php文件中:

 $formMapper
            ->with('General')
                ->add('username')
                ->add('email')
                ->add('plainPassword', 'text', array(
                    'required' => (!$this->getSubject() || is_null($this->getSubject()->getId()))
                ))
                ->add('nom')
            ->end()
            ->with('Groups')
                ->add('groups', 'sonata_type_model', array(
                    'required' => false,
                    'expanded' => true,
                    'multiple' => true
                ))
            ->end()

But I have the following error: 但是我有以下错误:

Please define a type for field `Prenom` in `Sonata\UserBundle\Admin\Entity\UserAdmin`

Thanks Best regards 最好的问候

Once you have generated the easy extend for sonata user bundle sonata creates orm files for entities eg if your extended bundle is generated in src/Application then your orm mapping files will be generated in src/Application/Sonata/UserBundle/Resources/config/doctrine as User.orm.xml or User.mongodb.xml depending on your database type add your field definition in doctrine mapping file 一旦生成了奏鸣曲用户捆绑包的简易扩展,奏鸣曲就为实体创建orm文件,例如,如果扩展捆绑包是在src/Application生成的,那么您的orm映射文件将在src/Application/Sonata/UserBundle/Resources/config/doctrine作为User.orm.xmlUser.mongodb.xml具体取决于您的数据库类型,将您的字段定义添加到理论映射文件中

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                  http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="Application\Sonata\UserBundle\Entity\User" table="fos user table name">
        <id name="id" column="id" type="integer">
            <generator strategy="AUTO" />
        </id>
        <field name="nom" type="string" column="title" length="255" nullable="false"/>
    </entity>
</doctrine-mapping>

If you don't need xml mapping and want to use annotations then rename the doctrine folder to something other like src/Application/Sonata/UserBundle/Resources/config/doctrine-backup now in your User entity add your field mapping in the form of annotation 如果您不需要xml映射并想使用注释,那么现在可以在User实体中将doctrine文件夹重命名为src/Application/Sonata/UserBundle/Resources/config/doctrine-backup之类的其他内容,以注解

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

/**
 * @ORM\Table(name="fos user table name")
 * @ORM\Entity
 */
class User extends BaseUser {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255, nullable=true)
     */
    protected $nom;

    /**
     * Set nom
     *
     * @param string $nom
     *
     * @return User
     */
    public function setNom( $nom) {
        $this->nom= $nom;

        return $this;
    }

    /**
     * Get nom
     *
     * @return string
     */
    public function getNom() {
        return $this->nom;
    }

}

Also one thing to note donot modify core files of the bundle like your error says Sonata\\UserBundle\\Admin\\Entity\\UserAdmin try to override main user admin class sonata's provides a way through user bundle configuration to define admin class 还需要注意的一件事是不要修改捆绑软件的核心文件,例如您的错误说Sonata\\UserBundle\\Admin\\Entity\\UserAdmin尝试覆盖主要用户管理类Sonata提供了一种通过用户捆绑软件配置来定义管理类的方法

sonata_user:
    class:                  # Entity Classes
        user:               Application\Sonata\UserBundle\Entity\User
        group:              Application\Sonata\UserBundle\Entity\Group

    admin:                  # Admin Classes
        user:
            class:          Application\Sonata\UserBundle\Admin\UserAdmin # there define your extended admin class
            controller:     SonataUserBundle:UserCURD
            translation:    SonataUserBundle

        group:
            class:          Sonata\UserBundle\Admin\Entity\GroupAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM