该关联指的是拥有侧字段e,其未被定义为关联,而是作为字段

时间:2018-01-15 01:45:54

标签: symfony doctrine-orm orm

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

但是当我想要链接两者时我遇到了错误:

  

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

     

[Mapping] FAIL - 实体类'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;
}

}

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

1 个答案:

答案 0 :(得分:0)

试试这个:

类文章:

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

Class commentaire_blog:

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

另外,请考虑将getCommentaires结果转换为Array:

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

要了解有关每个关系的拥有和反面的更多信息,请尝试阅读:here