Question

I am trying to access a blog based on a category title selected from a menu (see twig on bottom). When I select on the category I get the follow error as the category title is a string.

FatalErrorException: Error: Call to a member function getBlogs() on a non-object in /var/www/html/Symfony/src/AcmeBundle/Controller/PageController.php line 46

I have a ManyToOne and OneToMany relationship setup between Blog and Category posted below.

This part of the code that is triggering the error: $blog = $category->getBlogs;

Problem: What am I doing wrong and how can I access a blog and it's properties from the category title?

$category works correctly and brings up the correct Category based on the title, yet I cannot access the blog relationship which it's related to. When I dump $category I see this:

var_dump results on $category

array (size=1)
0 => 
object(AcmeBundle\Entity\Category)[515]
  private 'id' => int 1
  private 'title' => string 'Category 1' (length=10)
  protected 'blogs' => 
    object(Doctrine\ORM\PersistentCollection)[544]
      private 'snapshot' => 
        array (size=0)
          ...
      private 'owner' => 
        &object(AcmeBundle\Entity\Category)[515]
      private 'association' => 
        array (size=15)
          ...
      private 'em' => 
        object(Doctrine\ORM\EntityManager)[478]
          ...
      private 'backRefFieldName' => string 'category' (length=8)
      private 'typeClass' => 
        object(Doctrine\ORM\Mapping\ClassMetadata)[516]
          ...
      private 'isDirty' => boolean false
      private 'initialized' => boolean false
      private 'coll' => 
        object(Doctrine\Common\Collections\ArrayCollection)[545]
          ...

Controller:

/**
 * @Route("/category/{category}", name="AcmeBundle_category")
 * @Method({"GET"})
 * @Template("AcmeBundle:Page:category.html.twig")
 */
public function categoryAction($category = null)
{
    $em = $this->getDoctrine()->getManager();

    $category = $em->getRepository('AcmeBundle:Category')
        ->findBy(array(
            'title' => $category,
        ));

    $blog = $category->getBlogs();
    var_dump($category); die();
    return array(
        'blog' => 'blog',
    );
}

Category:

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

/**
 * Category
 *
 * @ORM\Table(name="category")
 * @ORM\Entity(repositoryClass="AcmeBundle\Entity\CategoryRepository")
 */
class Category
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255)
 */
private $title;

/**
 * @ORM\OneToMany(targetEntity="Blog", mappedBy="category")
 */
protected $blogs;

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


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

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

    return $this;
}

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

/**
 * Add blogs
 *
 * @param \AcmeBundle\Entity\Blog $blogs
 * @return Category
 */
public function addBlog(\AcmeBundle\Entity\Blog $blogs)
{
    $this->blogs[] = $blogs;

    return $this;
}

/**
 * Remove blogs
 *
 * @param \AcmeBundle\Entity\Blog $blogs
 */
public function removeBlog(\AcmeBundle\Entity\Blog $blogs)
{
    $this->blogs->removeElement($blogs);
}

/**
 * Get blogs
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getBlogs()
{
    return $this->blogs;
}
}

Blog

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="blogs")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;

/**
 * Set category
 *
 * @param \AcmeBundle\Entity\Category $category
 * @return Blog
 */
public function setCategory(\AcmeBundle\Entity\Category $category = null)
{
    $this->category = $category;

    return $this;
}

/**
 * Get category
 *
 * @return \AcmeBundle\Entity\Category 
 */
public function getCategory()
{
    return $this->category;
}

Twig

<a href="{{ path('AcmeBundle_category', { 'category': 'Category 1' }) }}">Category 1</a>

LoadBlogs Fixture

$blog1 = new Blog();
    $blog1->setCategory($manager->merge($this->getReference('category-1')));
    $blog1->setTitle('Lorem Ipsum2');
    $blog1->setBlog('Lorem ipsum dolor sit amet, consectetur adipisicing');
    $blog1->setImage('image.jpg');
    $blog1->setAuthor('Foo');
    $blog1->setCreated(new \DateTime("2014-04-23 21:03:02"));
    $blog1->setUpdated($blog1->getCreated());
    $manager->persist($blog1);
    $manager->flush();

public function getOrder()
{
    return 20;
}

LoadCategories Fixture

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use AcmeBundle\Entity\Category;

class LoadCategories extends AbstractFixture implements OrderedFixtureInterface    
$category1 = new Category();
    $category1->setTitle('Category 1');
    $manager->persist($category1);
    $manager->flush();

$this->addReference('category-1', $category1);

public function getOrder()
{
    return 10;
}
Was it helpful?

Solution

Your query returns an array not an object.

This will return a Category object.

$category = $em->getRepository('AcmeBundle:Category')
               ->findOneByTitle($category);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top