0

I have a question about extending Sonata UserBundle entity. I need to add more fields to the entity. Firstly, I extended Sonata UserBundle to my project on the folder src\Application\Sonata\UserBundle Then I tried to add these fields on the User Entity available on the same folder:

<?php

namespace App\Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

/**
 * This file has been generated by the SonataEasyExtendsBundle.
 *
 * @link https://sonata-project.org/easy-extends
 *
 * References:
 * @link http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
 */
class User extends BaseUser
{
    /**
     * @var int $id
     */
    protected $id;

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

    /**
     * @var string
     * @ORM\Column(type="string")
     */
    protected $accountType;

    /**
     * @var int
     * @ORM\ManyToOne(targetEntity="App\Entity\Specialty")
     */
    protected $specialty;

    /**
     * Get AccountType
     * @return string
     */
    public function getAccountType()
    {
        return $this->accountType;
    }

    /**
     * Set AccountType
     * @param string $accountType
     * @return $this
     */
    public function setAccountType($accountType)
    {
        $this->accountType = $accountType;

        return $this;
    }

    /**
     * Get Specialty
     * @return int
     */
    public function getSpecialty()
    {
        return $this->specialty;
    }

    /**
     * Set Specialty
     * @param int $specialty
     * @return Specialty
     */
    public function setSpecialty($specialty)
    {
        $this->specialty = $specialty;

        return $this;
    }
}

On the fos_user.yml I mapped this entity. But when I try to update my schema, by running this command:

php bin/console doctrine:s:u --force

I have a message that states that Nothing to update - your database is already in sync with the current entity metadata.

The added field isn't added on my table. I'm not an expert on Symfony, so I tried to explain my situation as possible as I can.

Specialty entity:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="specialties")
 * @ORM\Entity
 */
class Specialty
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Icon")
     */
    private $icon;

    /**
     * @ORM\Column(type="boolean")
     */
    private $status;

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

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

    /**
     * Set name
     * @param string $name
     * @return Specialty
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getIcon()
    {
        return $this->icon;
    }

    /**
     * @param mixed $icon
     * @return $this
     */
    public function setIcon($icon)
    {
        $this->icon = $icon;

        return $this;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function setStatus($status)
    {
        $this->status = $status;
        return $this;
    }

    public function __toString()
    {
        return $this->getId() ? (string) $this->getName() : '-';
    }
}

1 Answers1

0

Did you run php bin/console make:migration after adjusting the Entity?

  • php bin/console make:entity (create or update the Entity)
  • If you prefer to add new properties manually, the make:entity command can generate the getter & setter methods for you: php bin/console make:entity --regenerate
  • php bin/console make:migration
  • inspect src/Migrations/ folder
  • run the migrations php bin/console doctrine:migrations:migrate

read

AnC
  • 641
  • 4
  • 16
  • I do it. After runing command : `php bin/console make:entity --regenerate` nothing happen I have no update status displayed, even if I added some properties. – M'hamed Lalami May 27 '19 at 11:58
  • if you use `php bin/console make:entity --regenerate `make sure your annotations are correct. try it with `make:entity` to update the entity – AnC May 27 '19 at 13:53
  • There is an example of my personal user entity: `id; } /** * @var string * @ORM\Column(type="string", length=50, nullable=true) */ protected $spec; }` – M'hamed Lalami May 27 '19 at 15:05
  • I will take a closer look at this in the evening. Maybe check if the entity is valid in the meantime `bin/console doctrine:schema:validate` – AnC May 27 '19 at 15:43
  • I've tried to get sontana bundle running but faced multiple errors and i don't have the time to figure them out. Did you run the validate command? Can you also post the content of the "Specialty" entity? – AnC May 28 '19 at 17:19
  • Yes sure. I edited my first post adding the **Specialty** entity. – M'hamed Lalami May 28 '19 at 18:08