2012-01-24 30 views
3

我下面的symblog Symfony2的教程的第5部分:我該如何解決語義錯誤:「類沒有協會命名。」

http://tutorial.symblog.co.uk/docs/customising-the-view-more-with-twig.html 

標題下:首頁 - 博客和評論

當我得到更新:

// src/Blogger/BlogBundle/Repository/BlogRepositoy.php 

public function getLatestBlogs($limit = null) 
{ 
    $qb = $this->createQueryBuilder('b') 
      ->select('b, c') 
      ->leftJoin('b.comments', 'c') 
      ->addOrderBy('b.created', 'DESC'); 

    if (false === is_null($limit)) 
    $qb->setMaxResults($limit); 

    return $qb->getQuery() 
      ->getResult(); 
} 

而且當我更新:

{# src/Blogger/BlogBundle/Resources/views/Page/index.html.twig #} 

{# .. #} 

<footer class="meta"> 
    <p>Comments: <a href="{{ path('BloggerBlogBundle_blog_show', { 'id': blog.id }) }}#comments">{{ blog.comments|length }}</a></p> 
    <p>Posted by <span class="highlight">{{ blog.author }}</span> at {{ blog.created|date('h:iA') }}</p> 
    <p>Tags: <span class="highlight">{{ blog.tags }}</span></p> 
</footer> 

{# .. #} 

我再刷新我的瀏覽器,並得到錯誤:

[Semantical Error] line 0, col 71 near 'c ORDER BY b.created': Error: Class 
Blogger\BlogBundle\Entity\Blog has no association named comments 
500 Internal Server Error - QueryException 


<?php 
// src/Blogger/BlogBundle/Entity/Blog.php 

namespace Blogger\BlogBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository") 
* @ORM\Table(name="blog") 
* @ORM\HasLifecycleCallbacks() 
*/ 
class Blog 
{ 
public function __toString() 
{ 
    return $this->getTitle(); 
} 

/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\Column(type="string") 
*/ 
protected $title; 

/** 
* @ORM\Column(type="string", length=100) 
*/ 
protected $author; 

/** 
* @ORM\Column(type="text") 
*/ 
protected $blog; 

/** 
* @ORM\Column(type="string", length="20") 
*/ 
protected $image; 

/** 
* @ORM\Column(type="text") 
*/ 
protected $tags; 

protected $comments; 

/** 
* @ORM\Column(type="datetime") 
*/ 
protected $created; 

/** 
* @ORM\Column(type="datetime") 
*/ 
protected $updated; 


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

    $this->setCreated(new \DateTime()); 
    $this->setUpdated(new \DateTime()); 
} 

public function setUpdatedValue() 
{ 
    $this->setUpdated(new \DateTime()); 
} 

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

/** 
* Set title 
* 
* @param string $title 
*/ 
public function setTitle($title) 
{ 
    $this->title = $title; 
} 

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

/** 
* Set author 
* 
* @param string $author 
*/ 
public function setAuthor($author) 
{ 
    $this->author = $author; 
} 

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

/** 
* Set blog 
* 
* @param text $blog 
*/ 
public function setBlog($blog) 
{ 
    $this->blog = $blog; 
} 

/** 
* Get blog 
* 
* @return text 
*/ 
public function getBlog($length = null) 
{ 
    if (false === is_null($length) && $length > 0) 
     return substr($this->blog, 0, $length); 
    else 
     return $this->blog; 
} 

/** 
* Set image 
* 
* @param string $image 
*/ 
public function setImage($image) 
{ 
    $this->image = $image; 
} 

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

/** 
* Set tags 
* 
* @param text $tags 
*/ 
public function setTags($tags) 
{ 
    $this->tags = $tags; 
} 

/** 
* Get tags 
* 
* @return text 
*/ 
public function getTags() 
{ 
    return $this->tags; 
} 

/** 
* Set created 
* 
* @param datetime $created 
*/ 
public function setCreated($created) 
{ 
    $this->created = $created; 
} 

/** 
* Get created 
* 
* @return datetime 
*/ 
public function getCreated() 
{ 
    return $this->created; 
} 

/** 
* Set updated 
* 
* @param datetime $updated 
*/ 
public function setUpdated($updated) 
{ 
    $this->updated = $updated; 
} 

/** 
* Get updated 
* 
* @return datetime 
*/ 
public function getUpdated() 
{ 
    return $this->updated; 
} 
} 

請幫助解決這個問題。我不知道我錯在哪裏

感謝

+0

您可以發佈博客\ BlogBu​​ndle \實體\博客的內容是什麼? – Inoryy

+0

是的,請看頂部:) –

回答

6

您沒貼在src /博客/ BlogBu​​ndle /實體/ blog.php的文件。這將有助於解決您的問題。

很可能您沒有爲您的實體添加註釋字段(或沒有正確註釋它)。

類似的問題:Doctrine2: What is wrong with the association between these entities?

編輯:現在,當你粘貼你的實體可以看我的評論欄沒有被標註。學說的實體經理對此一無所知。你必須提供映射(在你的情況下通過註釋)。

編輯2:

在你的實體,你應該有(SRC /博客/ BlogBu​​ndle /實體/ blog.php的):

/** 
* @ORM\OneToMany(targetEntity="Comment", mappedBy="blog") 
*/ 
protected $comments; 

,但你必須:

protected $comments; 

映射不見了。學說不知道如何使用你的領域。

+0

我想你是指你的帖子底部;)我更新了我的答案。 –

+0

請看頂部:) –

+0

yikes我錯過了該教程中的內容嗎?我確定我跟着它到了T?我如何通過註釋添加映射?請在教程的哪個位置?第4部分我會想象? –

1

我也遵循symblog教程。我的blog.php文件如下:

<?php 

namespace Blogger\BlogBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* Blogger\BlogBundle\Entity\Blog 
*/ 
class Blog 
{ 
    /** 
    * @var integer $id 
    */ 
    private $id; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
{ 
    return $this->id; 
} 
/** 
* @var string $title 
*/ 
private $title; 

/** 
* @var string $author 
*/ 
private $author; 

/** 
* @var string $blog 
*/ 
private $blog; 

/** 
* @var string $image 
*/ 
private $image; 

/** 
* @var string $tags 
*/ 
private $tags; 

/** 
* @var string $comment 
*/ 
private $comment; 

/** 
* @var datetime $created 
*/ 
private $created; 

/** 
* @var datetime $updated 
*/ 
private $updated; 

public function __construct() 
{ 
    $this->comments = new ArrayCollection(); 
    $this->setCreated(new \DateTime()); 
    $this->setUpdated(new \DateTime()); 
} 
/** 
* Set title 
* 
* @param string $title 
*/ 
public function setTitle($title) 
{ 
    $this->title = $title; 
    $this->setSlug($this->title); 
} 

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

/** 
* Set author 
* 
* @param string $author 
*/ 
public function setAuthor($author) 
{ 
    $this->author = $author; 
} 

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

/** 
* Set blog 
* 
* @param string $blog 
*/ 
public function setBlog($blog) 
{ 
    $this->blog = $blog; 
} 

/** 
* Get blog 
* 
* @return string 
*/ 
public function getBlog($length = null) 
{ 
    if (false === is_null($length) && $length > 0) 
     return substr($this->blog, 0, $length); 
    else 
     return $this->blog; 
} 

/** 
* Set image 
* 
* @param string $image 
*/ 
public function setImage($image) 
{ 
    $this->image = $image; 
} 

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

/** 
* Set tags 
* 
* @param string $tags 
*/ 
public function setTags($tags) 
{ 
    $this->tags = $tags; 
} 

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

/** 
* Set comment 
* 
* @param string $comment 
*/ 
public function setComment($comment) 
{ 
    $this->comment = $comment; 
} 

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

/** 
* Set created 
* 
* @param datetime $created 
*/ 
public function setCreated($created) 
{ 
    $this->created = $created; 
} 

/** 
* Get created 
* 
* @return datetime 
*/ 
public function getCreated() 
{ 
    return $this->created; 
} 

/** 
* Set updated 
* 
* @param datetime $updated 
*/ 
public function setUpdated($updated) 
{ 
    $this->updated = $updated; 
} 

/** 
* Get updated 
* 
* @return datetime 
*/ 
public function getUpdated() 
{ 
    return $this->updated; 
} 

public function addComment(Comment $comment) 
{ 
    $this->comments[] = $comment; 
} 

public function getComments() 
{ 
    return $this->comments; 
} 

/** 
* @ORM\preUpdate 
*/ 
public function setUpdatedValue() 
{ 
    $this->setUpdated(new \DateTime()); 
} 

/** 
* @var Blogger\BlogBundle\Entity\Comment 
*/ 
private $comments; 

public function __toString() 
{ 
    return $this->getTitle(); 
} 

/** 
* @var string $slug 
*/ 
protected $slug; 


/** 
* Set slug 
* 
* @param string $slug 
*/ 
public function setSlug($slug) 
{ 
    $this->slug = $this->slugify($slug); 
} 

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

public function slugify($text) 
{ 
    $text = preg_replace('#[^\\pL\d]+#u', '-', $text); 

    $text = trim($text, '-'); 

    if (function_exists('iconv')) 
    { 
     $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); 
    } 

    $text = strtolower($text); 

    $text = preg_replace('#[^-\w]+#', '', $text); 

    if (empty($text)) 
    { 
     return 'n-a'; 
    } 

    return $text; 
    } } 

希望這對你有所幫助。

您的代碼缺少

/** 
* @var Blogger\BlogBundle\Entity\Comment 
*/ 
private $comments; 
相關問題