gpt4 book ai didi

symfony - 具有键 "getPageTitle"的数组的键 "0"不存在

转载 作者:行者123 更新时间:2023-12-02 12:55:28 25 4
gpt4 key购买 nike

我收到此错误

Key "getPageTitle" for array with keys "0" does not exist in DprocMainBundle:Dproc:single.html.twig at line 2

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

Controller

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

我做错了什么,我应该如何调用我的 getter 方法 getPageTitle?

谢谢

最佳答案

该错误源于您的 Doctrine 调用。如果你改变

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

至:

$page = $this->getDoctrine()
->getRepository('DprocMainBundle:Pages')
->findOneByPageTitle($slug);

你应该已经准备好了。问题在于“page”是一个页面数组(可能是一个只有单个页面的数组,但仍然是一个数组)。通过将方法更改为 findOneByX,您可以确保 Doctrine 返回单个对象,而不是对象数组。希望有帮助。

关于symfony - 具有键 "getPageTitle"的数组的键 "0"不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com