【问题标题】:Symfony:entities associationSymfony:实体关联
【发布时间】:2019-04-20 01:29:33
【问题描述】:

我有一个包含许多社交媒体的帖子,每个社交媒体在发布时都有一个链接,所以我做了 ManyToOne 和 OneTOMany,但我有这个错误:

类型“Doctrine\Common\Collections\Collection|array”的预期值 对于关联领域 “ProcessBundle\Entity\Publication#$Publication_Reseaux_Sociaux”,得到 改为“ProcessBundle\Entity\Reseaux_Sociaux”。

出版实体:

/**
  *@var ArrayCollection $Publication_Reseaux_Sociaux
  * @ORM\OneToMany(targetEntity="ProcessBundle\Entity\Publication_Reseaux_Sociaux",
 *     mappedBy="publication",cascade={"persist"})
 *
 */
 public $Publication_Reseaux_Sociaux;


Reseaux_Sociaux Entity:

/**
 *@var ArrayCollection $Publication_Reseaux_Sociaux
 * @ORM\OneToMany(targetEntity="ProcessBundle\Entity\Publication_Reseaux_Sociaux",
 *     mappedBy="Reseaux_Sociaux",cascade={"persist"})
 *
 */
private $Publication_Reseaux_Sociaux;
Publication_Reseaux_Sociaux Entity:
/**
 * @ORM\ManyToOne(targetEntity="ProcessBundle\Entity\Publication",
  *     inversedBy="Publication_Reseaux_Sociaux", cascade={"persist"})
 * @ORM\JoinColumn(name="publication_id", referencedColumnName="id")
   */
  protected $publication;
/**
 *  @var ArrayCollection
 * @ORM\ManyToOne(targetEntity="ProcessBundle\Entity\Reseaux_Sociaux",
 *     inversedBy="Publication_Reseaux_Sociaux",cascade={"persist"})
 * @ORM\JoinColumn(name="Reseaux_Sociaux_id", referencedColumnName="id")
 */
protected $Reseaux_Sociaux;
And this is a part of my code my form:
  ->add('Publication_Reseaux_Sociaux',EntityType::class, [
                'class'    =>Reseaux_Sociaux::class,
                'multiple' => true,
                'expanded' => true,
            ])

【问题讨论】:

    标签: symfony associations entities


    【解决方案1】:

    我在您的代码中看到的第一个问题:

    您使用以下注释定义 Publication_Resaux_Sociaux 字段:

    /**
     *@var ArrayCollection $Publication_Reseaux_Sociaux
     * @ORM\OneToMany(targetEntity="ProcessBundle\Entity\Publication_Reseaux_Sociaux",
     *     mappedBy="publication",cascade={"persist"})
     *
     */
     public $Publication_Reseaux_Sociaux;
    

    (我更喜欢camelCase 用于属性名称和CamelCase 用于类名,但我不会判断......)。因此,该字段的类与属性名称相同。但是,在您的表单中,该字段的类与字段名称不匹配(它应该):

      ->add('Publication_Reseaux_Sociaux', EntityType::class, [
                'class'    =>Reseaux_Sociaux::class, 
                'multiple' => true,
                'expanded' => true,
            ])
    

    也许这已经解决了您的问题。

    如果没有,您应该另外尝试以下方法:使用 CollectionType 和 entry_type EntityType。 CollectionType 将始终返回/设置集合/数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      相关资源
      最近更新 更多