Symfony:实体协会

问题描述 投票:0回答:1

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

关联字段“ProcessBundle \ Entity \ Publication#$ Publication_Reseaux_Sociaux”的“Doctrine \ Common \ Collections \ Collection | array”类型的预期值,取而代之的是“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个回答
0
投票

我用你的代码看到的第一个问题:

使用这些注释定义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用于属性Names和CamelCase用于类名,但我不会判断......)。因此该字段的类与属性名称相同。但是,在您的表单中,该字段的类与字段名称(它应该)不匹配:

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

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

如果没有,你还应该尝试以下方法:使用CollectionTypeentry_type EntityTypeCollectionType将始终返回/设置集合/数组。

© www.soinside.com 2019 - 2024. All rights reserved.