php - 为 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
调整Entity后你跑了吗?
- php bin/console make:entity(创建或更新实体)
- 如果您喜欢手动添加新属性,make:entity 命令可以为您生成 getter 和 setter 方法: php bin/console make:entity --regenerate
- php bin/console make:迁移
- 检查
src/Migrations/
文件夹 - 运行迁移
php bin/console doctrine:migrations:migrate
推荐阅读
- .htaccess - 避免 .htaccess 继承
- r - 插入符号分类阈值
- flutter - Flutter where 和 firstwhere 方法使用来自提供者的数据调用 null
- keycloak - 鳄梨酱注销功能不会注销网络浏览器
- python - 隐藏二叉树到双向链表(微软采访)
- c - 使用 fork 和 exec 启动 bin 程序
- unity3d - Raycast2D 仅命中 Collider 的一侧
- python - 如何在 python vtk 中连接两个单独的设置位置值?
- ocr - tesseract 在简单的数字检测中失败
- appstorage - 使用@AppStorage