【发布时间】:2021-12-06 08:51:48
【问题描述】:
用户可以选择他想要接收的电子邮件通知。
通知类型由枚举(MyCLabs 库)定义:
use MyCLabs\Enum\Enum;
final class NotificationType extends Enum
{
public const DEADLOCKED = 1;
public const REJECTED = 2;
public const SENT = 3;
public const ACCEPTED = 4;
public const REFUSED = 5;
public function translationPath(): string
{
return 'user.notifications.'.$this->getKey();
}
}
用户有更多的通知类型:
/**
* @ORM\Entity()
*/
class Notification
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="notifications")
*/
protected User $user;
/**
* @ORM\Id
* @ORM\Column(type="integer")
*/
protected int $notificationType;
public function __construct(User $user)
{
$this->user = $user;
}
public function getNotificationType(): NotificationType
{
return new NotificationType($this->notificationType);
}
public function setNotificationType(NotificationType $notificationType): self
{
$this->notificationType = $notificationType->getValue();
return $this;
}
}
用户实体:
/**
* @ORM\Entity()
*/
class User implements UserInterface, EquatableInterface
{
/**
* @var Collection|Notification[]
* @ORM\OneToMany(targetEntity=Notification::class, mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true)
*/
protected Collection $notifications;
//...
}
这是正确的解决方案吗?现在我在为复选框列表制作 Symfony 表单时遇到问题。
类似这样的事情(我知道,这是错误的):
$builder->add('notifications', ChoiceType::class, [
'choices' => NotificationType::values(),
'expanded' => true,
'multiple' => true,
'choice_value' => 'value',
'choice_label' => static function (NotificationType $type): string {
return $type->translationPath();
},
]);
我可以在我的情况下使用内置的 Symfony 表单吗?或者你对关系“实体多对多枚举”有更好的解决方案。
【问题讨论】:
-
你到底有什么问题/错误?
-
问题:表单字段“通知”预期的 NotificationType 实例数组(选择选项)。但是给定通知实体的集合。
标签: php symfony doctrine symfony-forms