簡體   English   中英

添加Sonata UserBundle的字段

[英]Add fields for Sonata UserBundle

我有關於擴展Sonata UserBundle實體的問題。 我需要向實體添加更多字段。 首先,我將Sonata UserBundle擴展到文件夾src \\ Application \\ Sonata \\ UserBundle上的項目然后我嘗試在同一文件夾上可用的用戶實體上添加這些字段:

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

在fos_user.yml上我映射了這個實體。 但是當我嘗試通過運行此命令來更新我的架構時:

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

我有一條消息說明無需更新 - 您的數據庫已與當前實體元數據同步。

添加的字段未添加到我的表格中。 我不是Symfony的專家,所以我試着盡可能地解釋我的情況。

專業實體:

<?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() : '-';
    }
}

調整實體后,您是否運行了php bin/console make:migration

  • php bin / console make:entity(創建或更新實體)
  • 如果您希望手動添加新屬性,make:entity命令可以為您生成getter和setter方法:php bin / console make:entity --regenerate
  • php bin / console make:migration
  • 檢查src/Migrations/文件夾
  • 運行遷移php bin/console doctrine:migrations:migrate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM