Symfony2 -不能从类别标题访问博客(多对一关系)



我正在尝试访问基于从菜单中选择的类别标题的博客(见底部的小树枝)。当我选择类别时,我得到以下错误,因为类别标题是字符串。

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

我有一个多对一和多对一的关系设置博客和分类张贴下面。

触发错误的这部分代码:$blog = $category->getBlogs;

问题:我做错了什么,我怎么能从类别标题访问博客和它的属性?

$category工作正常,并根据标题显示正确的category,但我无法访问与之相关的博客关系。当我转储$category时,我看到如下:

var_dump $category

array (size=1)
0 => 
object(AcmeBundleEntityCategory)[515]
  private 'id' => int 1
  private 'title' => string 'Category 1' (length=10)
  protected 'blogs' => 
    object(DoctrineORMPersistentCollection)[544]
      private 'snapshot' => 
        array (size=0)
          ...
      private 'owner' => 
        &object(AcmeBundleEntityCategory)[515]
      private 'association' => 
        array (size=15)
          ...
      private 'em' => 
        object(DoctrineORMEntityManager)[478]
          ...
      private 'backRefFieldName' => string 'category' (length=8)
      private 'typeClass' => 
        object(DoctrineORMMappingClassMetadata)[516]
          ...
      private 'isDirty' => boolean false
      private 'initialized' => boolean false
      private 'coll' => 
        object(DoctrineCommonCollectionsArrayCollection)[545]
          ...

控制器:

/**
 * @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',
    );
}

类别:

use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
 * Category
 *
 * @ORMTable(name="category")
 * @ORMEntity(repositoryClass="AcmeBundleEntityCategoryRepository")
 */
class Category
{
/**
 * @var integer
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var string
 *
 * @ORMColumn(name="title", type="string", length=255)
 */
private $title;
/**
 * @ORMOneToMany(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 AcmeBundleEntityBlog $blogs
 * @return Category
 */
public function addBlog(AcmeBundleEntityBlog $blogs)
{
    $this->blogs[] = $blogs;
    return $this;
}
/**
 * Remove blogs
 *
 * @param AcmeBundleEntityBlog $blogs
 */
public function removeBlog(AcmeBundleEntityBlog $blogs)
{
    $this->blogs->removeElement($blogs);
}
/**
 * Get blogs
 *
 * @return DoctrineCommonCollectionsCollection 
 */
public function getBlogs()
{
    return $this->blogs;
}
}

/**
 * @ORMManyToOne(targetEntity="Category", inversedBy="blogs")
 * @ORMJoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;
/**
 * Set category
 *
 * @param AcmeBundleEntityCategory $category
 * @return Blog
 */
public function setCategory(AcmeBundleEntityCategory $category = null)
{
    $this->category = $category;
    return $this;
}
/**
 * Get category
 *
 * @return AcmeBundleEntityCategory 
 */
public function getCategory()
{
    return $this->category;
}
树枝>
<a href="{{ path('AcmeBundle_category', { 'category': 'Category 1' }) }}">Category 1</a>

LoadBlogs夹具

$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夹具

use DoctrineCommonDataFixturesAbstractFixture;
use DoctrineCommonPersistenceObjectManager;
use DoctrineCommonDataFixturesOrderedFixtureInterface;
use AcmeBundleEntityCategory;
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;
}

您的查询返回一个数组,而不是一个对象。

返回一个Category对象。

$category = $em->getRepository('AcmeBundle:Category')
               ->findOneByTitle($category);

最新更新