加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 我如何解决语义错误:“类没有名称的关联..”

发布时间:2020-12-13 21:45:13 所属栏目:PHP教程 来源:网络整理
导读:我正在遵循symblog symfony2教程的第5部分: http://tutorial.symblog.co.uk/docs/customising-the-view-more-with-twig.html 标题下:主页 – 博客和评论 当我得到更新时: // src/Blogger/BlogBundle/Repository/BlogRepositoy.phppublic function getLate
我正在遵循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   
BloggerBlogBundleEntityBlog has no association named comments
500 Internal Server Error - QueryException


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

 namespace BloggerBlogBundleEntity;

 use DoctrineORMMapping as ORM;
 use DoctrineCommonCollectionsArrayCollection;

/**
 * @ORMEntity(repositoryClass="BloggerBlogBundleRepositoryBlogRepository")
 * @ORMTable(name="blog")
 * @ORMHasLifecycleCallbacks()
 */
class Blog
{
public function __toString()
{
    return $this->getTitle();
}

/**
 * @ORMId
 * @ORMColumn(type="integer")
 * @ORMGeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORMColumn(type="string")
 */
protected $title;

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

/**
 * @ORMColumn(type="text")
 */
protected $blog;

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

/**
 * @ORMColumn(type="text")
 */
protected $tags;

protected $comments;

/**
 * @ORMColumn(type="datetime")
 */
protected $created;

/**
 * @ORMColumn(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,$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;
}
}

请帮忙解决这个问题.我不知道我哪里出错了

谢谢

解决方法

您没有粘贴src / Blogger / BlogBu??ndle / Entity / Blog.php文件.这有助于解决您的问题.

很可能你没有为你的实体添加注释字段(或者没有正确地注释它).

类似的问题:Doctrine2: What is wrong with the association between these entities?

编辑:现在当你粘贴你的实体时,我可以看到注释字段没有注释. Doctrine的实体经理对此一无所知.您必须提供映射(在您的情况下通过注释).

编辑2:

在你的实体中你应该有(src / Blogger / BlogBu??ndle / Entity / Blog.php):

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

但是你有:

protected $comments;

缺少映射.学说不知道如何使用你的领域.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读