【问题标题】:The associationrefers to the owning side field e which is not defined as association, but as field关联是指拥有方字段e,它不是定义为关联,而是定义为字段
【发布时间】:2018-01-15 01:45:54
【问题描述】:

我正在制作一个博客项目。所以我有一个文章类和一个评论博客类。

但是当我想链接两者时出现错误:

[Mapping] FAIL - 实体类 'AppBundle\Entity\Article' 映射 是无效的: * 关联 AppBundle\Entity\Article#commentaires 是指拥有方字段 AppBundle\Entity\Commentaire_blog#message,即 不定义为关联,而是定义为字段。 * 关联 AppBundle\Entity\Article#commentaires 指的是拥有方字段 AppBundle\Entity\Commentaire_blog#message 不存在。

[映射] 失败 - 实体类 'AppBundle\Entity\Commentaire_blog' 映射无效: * 映射 AppBundle\Entity\Commentaire_blog#article 和 AppBundle\Entity\Article#commentaires 各自不一致 其他。

我有点迷茫……

这是我的两个类:

 * Article
 *
 * @ORM\Table(name="article")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ArticleRepository")
 */
 class Article
 {
 /**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="Titre", type="text")
 */
private $titre;

/**
 * @var string
 *
 * @ORM\Column(name="article", type="text")
 */
private $article;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="date_article", type="datetime")
 */
private $dateArticle;

/**
 * @ORM\OneToMany(targetEntity="Commentaire_blog", mappedBy="messa")
 */
private $commentaires;


public function __construct()
{
    $this->commentaires = new ArrayCollection();

}

/**
 * @return Collection|Commentaire_blog[]
 */
public function getCommentaires()
{
    return $this->commentaires;
}

/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set titre
 *
 * @param string $titre
 *
 * @return Article
 */
public function setTitre($titre)
{
    $this->titre = $titre;

    return $this;
}

/**
 * Get titre
 *
 * @return string
 */
public function getTitre()
{
    return $this->titre;
}

/**
 * Set article
 *
 * @param string $article
 *
 * @return Article
 */
public function setArticle($article)
{
    $this->article = $article;

    return $this;
}

/**
 * Get article
 *
 * @return string
 */
public function getArticle()
{
    return $this->article;
}

/**
 * Set dateArticle
 *
 * @param \DateTime $dateArticle
 *
 * @return Article
 */
public function setDateArticle($dateArticle)
{
    $this->dateArticle = $dateArticle;

    return $this;
}

/**
 * Get dateArticle
 *
 * @return \DateTime
 */
public function getDateArticle()
{
    return $this->dateArticle;
}

第二个:

* Commentaire_blog
*
* @ORM\Table(name="commentaire_blog")
* @ORM\Entity(repositoryClass="AppBundle\Repository      \Commentaire_blogRepository")
*/
class Commentaire_blog
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="usernamne", type="string", length=255)
 */
private $usernamne;

/**
 * @var string
 *
 * @ORM\Column(name="message", type="text")
 */
private $message;

/**
 * @var bool
 *
 * @ORM\Column(name="is_visible", type="boolean")
 */
private $isVisible;

/**
 * @return mixed
 */
public function getArticle()
{
    return $this->article;
}

/**
 * @param mixed $article
 */
public function setArticle($article)
{
    $this->article = $article;
}

/**
 * @ORM\ManyToOne(targetEntity="Article", inversedBy="commentaires")
 * @ORM\JoinColumn(nullable=true)
 */
private $article;

/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set usernamne
 *
 * @param string $usernamne
 *
 * @return Commentaire_blog
 */
public function setUsernamne($usernamne)
{
    $this->usernamne = $usernamne;

    return $this;
}

/**
 * Get usernamne
 *
 * @return string
 */
public function getUsernamne()
{
    return $this->usernamne;
}

/**
 * Set message
 *
 * @param string $message
 *
 * @return Commentaire_blog
 */
public function setMessage($message)
{
    $this->message = $message;

    return $this;
}

/**
 * Get message
 *
 * @return string
 */
public function getMessage()
{
    return $this->message;
}

/**
 * Set isVisible
 *
 * @param boolean $isVisible
 *
 * @return Commentaire_blog
 */
public function setIsVisible($isVisible)
{
    $this->isVisible = $isVisible;

    return $this;
}

/**
 * Get isVisible
 *
 * @return bool
 */
public function getIsVisible()
{
    return $this->isVisible;
}

}

任何想法如何使链接正确...

【问题讨论】:

    标签: symfony doctrine-orm orm


    【解决方案1】:

    试试这个:

    类文章:

    /**
     * @ORM\OneToMany(targetEntity="Commentaire_blog", mappedBy="article")
     */
    private $commentaires;
    

    类commentaire_blog:

    /**
     * @ORM\ManyToOne(targetEntity="Article", inversedBy="commentaires")
     * @ORM\JoinColumn(nullable=true)
     */
    private $article;
    

    另外,考虑将 getCommentaires 结果转换为数组:

    public function getCommentaires()
    {
        return $this->commentaires->toArray();
    }
    

    要了解有关每个关系的拥有和反面的更多信息,请阅读以下内容:Associations: Owning and Inverse Side

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 1970-01-01
      • 2013-05-22
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      相关资源
      最近更新 更多