我正在尝试根据从菜单中选择的类别标题访问博客(请参阅底部的twig)。当我选择类别时,我得到了跟随错误,因为类别标题是一个字符串。
FatalErrorException: Error: Call to a member function getBlogs() on a non-object in /var/www/html/Symfony/src/AcmeBundle/Controller/PageController.php line 46
我在下面发布的博客和类别之间设置了ManyToOne和OneToMany关系。
触发错误的这部分代码:$blog = $category->getBlogs;
问题:我做错了什么,如何从类别标题访问博客及其属性?
$ category正常运行并根据标题显示正确的类别,但我无法访问与其相关的博客关系。当我转储$ category时,我看到了:
var_dump结果为$ 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]
...
控制器:
/**
* @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 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;
}
}
博客
/**
* @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;
}
枝条
<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;
}
答案 0 :(得分:1)
您的查询返回的数组不是对象。
这将返回一个Category对象。
$category = $em->getRepository('AcmeBundle:Category')
->findOneByTitle($category);