Symfony / Doctrine 2.x - 显示表单 EntityType::class 用户列表 - 保存@OneToMany - 错误
Symfony / Doctrine 2.x - Show form EntityType::class list of users - save @OneToMany - error
使用 Symfony 4.1,Doctrine 2.x,我有三个实体。一个 Subscription 可以分配多个 Identities(又名用户帐户)。当保存哪些身份属于屏幕上当前可见的订阅时(通过标准的 Symfony 形式),它失败了。
Identity.php
class Identity implements UserInterface, \Serializable, InteractiveLoginUserInterface
{
/**
* @var int
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\NotBlank()
* @Assert\Email()
* @Assert\Length(max="255")
*/
public $username;
Subscription.php
class Subscription
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
/**
* @var Collection
* @ORM\OneToMany(targetEntity="SubscriptionIdentity", mappedBy="subscription", fetch="EAGER")
*/
public $identities;
订阅Identity.php
class SubscriptionIdentity
{
/**
* @var int
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Subscription", inversedBy="identities")
* @ORM\JoinColumn(name="subscription_id", referencedColumnName="id", nullable=false)
*/
public $subscription;
/**
* @ORM\ManyToOne(targetEntity="Identity", inversedBy="subscriptions")
* @ORM\JoinColumn(name="identity_id", referencedColumnName="id", nullable=true)
*/
public $identity;
我有以下表格
class GeneralSettingsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
....<snip>....
->add('identities', EntityType::class, [
'label' => 'Who can view this subscription',
// looks for choices from this entity
'class' => Identity::class,
//'placeholder' => 'Choose an option',
// uses the User.username property as the visible option string
'choice_label' => 'username',
'multiple' => true,
这适用于为最终用户显示选项列表框 select 来自:
EntityType form field
当我保存我的表格时,我遇到了这个问题:
ORMInvalidArgumentException
HTTP 500 Internal Server Error
Expected value of type "App\Entity\**SubscriptionIdentity**" for association field
"App\Entity\Subscription#$identities", got "App\Entity\Identity" instead.
请帮忙!在这种情况下,Symfony / Doctrine 文档并没有太大帮助。谢谢。
您收到此错误是因为您尝试将 Identity
类型的对象分配给一个属性,其中 doctrine waiting un object off type SubscriptionIdentity
.
你这里描述的是多对多的关系。
尝试像这样修改您的实体:
class Subscription
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
/**
* @var Collection
* @ManyToMany(targetEntity="Identity")
* @JoinTable(name="subscription_identity",
* joinColumns={@JoinColumn(name="subscription_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="identity_id", referencedColumnName="id")}
* )
*/
public $identities;
使用 Symfony 4.1,Doctrine 2.x,我有三个实体。一个 Subscription 可以分配多个 Identities(又名用户帐户)。当保存哪些身份属于屏幕上当前可见的订阅时(通过标准的 Symfony 形式),它失败了。
Identity.php
class Identity implements UserInterface, \Serializable, InteractiveLoginUserInterface
{
/**
* @var int
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\NotBlank()
* @Assert\Email()
* @Assert\Length(max="255")
*/
public $username;
Subscription.php
class Subscription
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
/**
* @var Collection
* @ORM\OneToMany(targetEntity="SubscriptionIdentity", mappedBy="subscription", fetch="EAGER")
*/
public $identities;
订阅Identity.php
class SubscriptionIdentity
{
/**
* @var int
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Subscription", inversedBy="identities")
* @ORM\JoinColumn(name="subscription_id", referencedColumnName="id", nullable=false)
*/
public $subscription;
/**
* @ORM\ManyToOne(targetEntity="Identity", inversedBy="subscriptions")
* @ORM\JoinColumn(name="identity_id", referencedColumnName="id", nullable=true)
*/
public $identity;
我有以下表格
class GeneralSettingsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
....<snip>....
->add('identities', EntityType::class, [
'label' => 'Who can view this subscription',
// looks for choices from this entity
'class' => Identity::class,
//'placeholder' => 'Choose an option',
// uses the User.username property as the visible option string
'choice_label' => 'username',
'multiple' => true,
这适用于为最终用户显示选项列表框 select 来自:
EntityType form field
当我保存我的表格时,我遇到了这个问题:
ORMInvalidArgumentException
HTTP 500 Internal Server Error
Expected value of type "App\Entity\**SubscriptionIdentity**" for association field
"App\Entity\Subscription#$identities", got "App\Entity\Identity" instead.
请帮忙!在这种情况下,Symfony / Doctrine 文档并没有太大帮助。谢谢。
您收到此错误是因为您尝试将 Identity
类型的对象分配给一个属性,其中 doctrine waiting un object off type SubscriptionIdentity
.
你这里描述的是多对多的关系。
尝试像这样修改您的实体:
class Subscription
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
/**
* @var Collection
* @ManyToMany(targetEntity="Identity")
* @JoinTable(name="subscription_identity",
* joinColumns={@JoinColumn(name="subscription_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="identity_id", referencedColumnName="id")}
* )
*/
public $identities;