【问题标题】:How do i solve Semantical error: "Class has no association named.."我如何解决语义错误:“类没有命名的关联..”
【发布时间】:2012-01-24 07:24:53
【问题描述】:

我正在关注 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;
}
}

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

谢谢

【问题讨论】:

  • 您可以发布 Blogger\BlogBu​​ndle\Entity\Blog 的内容吗?

标签: php symfony doctrine-orm


【解决方案1】:

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

很可能您没有将 cmets 字段添加到您的实体(或者没有正确注释它)。

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

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

编辑2:

在您的实体中,您应该拥有 (src/Blogger/BlogBu​​ndle/Entity/Blog.php):

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

但你有:

protected $comments;

缺少映射。 Doctrine 不知道如何使用你的领域。

【讨论】:

  • 哎呀,我错过了该教程中的某些内容吗?我确定我跟着它去了T?如何通过注释添加映射?请问教程在哪里?第 4 部分我会想象?
  • 我现在看到第 4 部分它说:更新位于 src/Blogger/BlogBu​​ndle/Entity/Blog.php 的博客实体以添加此映射。我必须添加到代码中的行是: $this->cmets = new ArrayCollection();我在我的 Blog.php
  • 我在第 4 部分安装 php bin/vendors 时遇到问题:[ErrorException] 警告:重命名(/home/helloises/symfony2/Symfony/app/cache/dev,/home/helloises/symfony2/ Symfony/app/cache/dev_old):/home/helloises/symfony2/Symfony/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php 第 68 行中的目录不为空,请帮忙?
  • 谢谢!我添加了 /**...**/ 部分..当我刷新浏览器时出现的错误是:BloggerBlogBu​​ndle 中不存在对象“Blogger\BlogBu​​ndle\Entity\Blog”的方法“cmets”:页面:第 19 行的 index.html.twig 以及上面的安装供应商问题:)
【解决方案2】:

我也遵循了 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;

【讨论】:

    猜你喜欢
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多