Search code examples
phpsymfonydoctrine-orm

How do i solve Semantical error: "Class has no association named.."


i am following part 5 of the symblog symfony2 tutorial:

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

under heading: The Homepage - Blogs and Comments

when i get to update:

// 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();
}

and also when i update:

{# 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>

{# .. #}

i then refresh my browser and get error:

[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;
}
}

please help solve this. i dont know where i went wrong

thanks


Solution

  • You didn't paste the src/Blogger/BlogBundle/Entity/Blog.php file. It would help resolving your issue.

    Most probably you didn't add comments field to your entity (or didn't annotate it properly).

    Similar problem: Doctrine2: What is wrong with the association between these entities?

    EDIT: Now when you pasted your entity I can see the comments field is not annotated. Doctrine's entity manager doesn't know anything about it. You have to provide mapping (in your case via annotations).

    EDIT 2:

    In your entity you should have (src/Blogger/BlogBundle/Entity/Blog.php):

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

    but you have:

    protected $comments;
    

    Mapping is missing. Doctrine doesn't know how to use your field.