财产"评论"也没有一种方法存在,并且在课堂上有公共访问权限

时间:2018-01-14 16:50:20

标签: symfony orm

我目前正在使用Symfony 3构建博客 我有2个大班 文章 - >我的博客文章

<div class="logo">
  <a href="#">
  <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 297 297" xml:space="preserve" preserveAspectRatio="xMinYMin">
    <g>
      <path d="M148.5,212.002c-5.458,0-9.884,4.425-9.884,9.883v32.616c0,5.458,4.426,9.884,9.884,9.884c5.458,0,9.884-4.426,9.884-9.884
		v-32.616C158.384,216.427,153.958,212.002,148.5,212.002z" />
      <path d="M104.922,220.55c-0.737-5.408-5.71-9.194-11.129-8.457c-5.408,0.737-9.194,5.72-8.457,11.128l4.448,32.616
		c0.675,4.955,4.914,8.549,9.78,8.549c0.444,0,0.895-0.03,1.348-0.092c5.408-0.737,9.194-5.72,8.457-11.129L104.922,220.55z" />
      <path d="M203.207,212.093c-5.414-0.739-10.392,3.05-11.129,8.457l-4.447,32.615c-0.737,5.409,3.049,10.392,8.457,11.129
		c0.453,0.062,0.903,0.092,1.348,0.092c4.866,0,9.105-3.595,9.78-8.549l4.448-32.616
		C212.401,217.813,208.615,212.83,203.207,212.093z" />
      <path d="M289.359,156.654c0-30.362-15.107-62.087-41.448-87.04c-16.82-15.936-36.479-27.493-57.5-34.118
		C187.061,15.383,169.548,0,148.5,0s-38.562,15.383-41.911,35.496c-21.021,6.625-40.68,18.183-57.5,34.118
		c-26.342,24.953-41.448,56.678-41.448,87.04c0,18.932,12.597,35.007,29.942,40.486l7.808,63.71
		C47.835,280.784,66.161,297,86.241,297h124.518c20.08,0,38.406-16.216,40.851-36.149l7.793-63.588
		C276.733,191.898,289.359,175.725,289.359,156.654z M148.5,19.767c12.535,0,22.732,10.197,22.732,22.732
		c0,12.535-10.197,22.732-22.732,22.732s-22.732-10.197-22.732-22.732C125.768,29.964,135.965,19.767,148.5,19.767z
		 M231.989,258.445c-1.249,10.184-10.972,18.788-21.23,18.788H86.241c-10.259,0-19.981-8.604-21.23-18.788l-7.338-59.877
		c10.252-1.711,19.274-7.102,25.654-14.768c7.897,9.375,19.791,15.353,33.07,15.353c13.01,0,24.664-5.946,32.419-15.281
		c7.849,9.335,19.644,15.281,32.813,15.281c13.01,0,24.664-5.946,32.419-15.281c6.319,7.515,15.198,12.826,25.289,14.615
		L231.989,258.445z M246.86,179.386c-12.676,0-22.989-10.197-22.989-22.731c0-5.459-4.425-9.884-9.883-9.884
		c-5.458,0-9.884,4.425-9.884,9.884c0,12.534-10.082,22.731-22.476,22.731c-12.676,0-22.988-10.197-22.988-22.731
		c0-5.459-4.426-9.884-9.884-9.884c-5.458,0-9.884,4.425-9.884,9.884c0,12.534-10.082,22.731-22.476,22.731
		c-12.601,0-23.245-10.409-23.245-22.731c0-5.459-4.426-9.884-9.884-9.884c-5.458,0-9.884,4.425-9.884,9.884
		c0,12.534-10.197,22.731-22.731,22.731c-12.601,0-23.246-10.409-23.246-22.731c0-24.592,13.188-51.766,35.276-72.689
		c13.436-12.73,28.928-22.233,45.469-28.142c5.604,16.925,21.567,29.175,40.348,29.175s34.744-12.25,40.348-29.175
		c16.542,5.908,32.032,15.411,45.469,28.142c22.089,20.924,35.276,48.098,35.276,72.689
		C269.593,169.189,259.396,179.386,246.86,179.386z" />
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
  </svg>
  <h2>Muffin-Welt.de</h2>
  </a>
</div>

Commentaire_blog - &gt;随着我的博客评论。

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * 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="article")
 */
private $commentaires;


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

/**
 * @return mixed
 */
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;
}


}

当我想要显示我的页面时,我有这个错误:

  

财产&#34;评论&#34;也不是其中一种方法&#34; getCommentaire()&#34;,&#34; commentaire()&#34;,&#34; isCommentaire()&#34;,&#34; hasCommentaire()&# 34;,&#34; __ get()&#34;在课堂上存在并拥有公共访问权限#34; AppBundle \ Entity \ Commentaire_blog&#34;。

我不明白这个问题......所以如果有人能解释我会发生什么事情会很好。

Thanx很多

2 个答案:

答案 0 :(得分:1)

OneToMany关系必须是双向的,或使用official documentation

中所述的联接表
  

一对多关联必须是双向的,除非你是   使用连接表。这是因为在一对多的许多方面   协会拥有外键,使其成为拥有者。教义   需要定义多方才能理解这种关联。

这意味着您必须在ManyToOne上定义Commentaire_blog->commentaire关系,或使用here所述的联接表将Article->commentaire声明为ManyToMany关系

/**
 * @ORM\ManyToMany(targetEntity="Commentaire_blog")
 * @JoinTable(name="articles_commentaireblogs",
 *      joinColumns={@JoinColumn(name="article_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="commentaire_blog_id", referencedColumnName="id", unique=true)}
 */
private $commentaires;

答案 1 :(得分:0)

将这两种方法添加到一个结束类Article

/**
 * Add a Commentaire_blog to this Article
 *
 * @param \AppBundle\Entity\Commentaire_blog $commentaireBlog
 */
public function addCommentaire(Commentaire_blog $commentaireBlog)
{
    if (!$this->commentaires->contains($commentaireBlog)) {
        $this->commentaires->add($commentaireBlog);
    }
}

/**
 * Remove, if exists, a Commentaire_blog from this Article
 *
 * @param \AppBundle\Entity\Commentaire_blog $commentaireBlog
 */
public function removeCommentaire(Commentaire_blog $commentaireBlog)
{
    if ($this->commentaires->contains($commentaireBlog)) {
        $this->commentaires->removeElement($commentaireBlog);
    }
}

我建议你编辑Article->commentaires属性,以允许Article班级创建,删除和编辑与之相关的评论:

/**
 * @ORM\OneToMany(targetEntity="Commentaire_blog", mappedBy="article",
 *     cascade={"persist", "remove", "merge"}
 * )
 */
private $commentaires;

修改

尝试将此方法添加到Article类。别忘了添加上面的方法,意识到我已经改变了上面方法的名称。

/**
 * Get a Commentaire_blog of this Article
 *
 * @param \AppBundle\Entity\Commentaire_blog $commentaireBlog
 */
public function GetCommentaire(Commentaire_blog $commentaireBlog)
{
    if (!$this->commentaires->contains($commentaireBlog)) {
        return $this->commentaires->get($commentaireBlog);
    }
}