【问题标题】:Symfony2 - Cannot access blog from Category title (ManyToOne relationship)Symfony2 - 无法从类别标题访问博客(多对一关系)
【发布时间】:2014-06-28 11:39:59
【问题描述】:

我正在尝试根据从菜单中选择的类别标题访问博客(请参阅底部的树枝)。当我选择类别时,我收到以下错误,因为类别标题是一个字符串。

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 之间设置了 ManyToOne 和 OneToMany 关系。

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

问题:我做错了什么以及如何从类别标题访问博客及其属性?

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

$category 上的 var_dump 结果

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

加载类别夹具

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

【问题讨论】:

  • 错误表明您正在尝试对非对象使用方法,这意味着您的类别存储库上的方法“findBy”没有返回结果。验证您的数据库中是否有条目,以及传递给 findBy 的值是否正确。

标签: symfony doctrine-orm blogs entity-relationship categories


【解决方案1】:

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

这将返回一个类别对象。

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

【讨论】:

  • 完美,这将返回每个类别的博客。谢谢大卫。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多