简体   繁体   中英

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.

I have then tried to modify the Entity\\User.php file:

<?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:

 $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

<?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

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_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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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