键"0"的数组的键"getPageTitle"不存在

M M*_*ero 3 doctrine symfony

我收到了这个错误

DprocMainBundle中不存在键为"0"的数组的"getPageTitle"键:Dproc:第2行的single.html.twig

第2行: {{ page.getPageTitle }}

我的实体文件

<?php
namespace Dproc\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @IgnoreAnnotation("fn")
 *
 */
/**
 * @ORM\Entity
 * @ORM\Table(name="pages")
 */
class Pages
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $Id;

    /**
    * @ORM\Column(name="page_title", type="text")
    */
    protected $pageTitle;

    /**
 * @ORM\Column(name="page_content", type="text")
 */
    protected $pageContent;

    /**
    * @ORM\Column(name="page_category", type="text")
    */
    protected $pageCategory;

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

    /**
     * Set page_title
     *
     * @param string $pageTitle
     * @return Pages
     */
    public function setPageTitle($pageTitle)
    {
        $this->pageTitle = $pageTitle;

        return $this;
    }

    /**
     * Get page_title
     *
     * @return string 
     */
    public function getPageTitle()
    {
        return $this->pageTitle;
    }

    /**
     * Set page_content
     *
     * @param string $pageContent
     * @return Pages
     */
    public function setPageContent($pageContent)
    {
        $this->pageContent = $pageContent;

        return $this;
    }

    /**
     * Get page_content
     *
     * @return string 
     */
    public function getPageContent()
    {
        return $this->pageContent;
    }

    /**
     * Set page_category
     *
     * @param string $pageCategory
     * @return Pages
     */
    public function setPageCategory($pageCategory)
    {
        $this->pageCategory = $pageCategory;

        return $this;
    }

    /**
     * Get page_category
     *
     * @return string 
     */
    public function getPageCategory()
    {
        return $this->pageCategory;
    }
}
Run Code Online (Sandbox Code Playgroud)

调节器

public function IndexAction($slug)
    {
        $page = $this->getDoctrine()
           ->getRepository('DprocMainBundle:Pages')
           ->findByPageTitle($slug);

        if (!$page) {
           throw $this->createNotFoundException('No product found for slug '.$slug);
        }
        //print_r($page);
        return $this->render('DprocMainBundle:Dproc:single.html.twig',array('page' => $page));
    }
Run Code Online (Sandbox Code Playgroud)

我做错了什么,我怎么称呼我的getter方法getPageTitle?

谢谢

小智 14

错误源于你的学说.如果你改变了

$page = $this->getDoctrine()
       ->getRepository('DprocMainBundle:Pages')
       ->findByPageTitle($slug);
Run Code Online (Sandbox Code Playgroud)

至:

$page = $this->getDoctrine()
       ->getRepository('DprocMainBundle:Pages')
       ->findOneByPageTitle($slug);
Run Code Online (Sandbox Code Playgroud)

你应该全力以赴.问题是'page'是一个页面数组(可能是一个只有一个页面的数组,但仍然是一个数组).通过将方法更改为findOneByX,您可以确保Doctrine将返回单个对象,而不是对象数组.希望有所帮助.