Php 条令一个接一个';日期和日期
我正在使用Symfony 2.7.1,在使用我的新闻实体时似乎有问题。我正在尝试在我的小树枝模板中使用已发布的 我尝试使用Php 条令一个接一个';日期和日期,php,symfony,doctrine-orm,entity,Php,Symfony,Doctrine Orm,Entity,我正在使用Symfony 2.7.1,在使用我的新闻实体时似乎有问题。我正在尝试在我的小树枝模板中使用已发布的 我尝试使用{{news\u item.published\u at | date(“m/d/Y”)}},但随后出现了一个致命错误: Method "published_at" for object "AppBundle\Entity\News" does not exist in AppBundle:news:index.html.twig at line 7 第7行: {{news
{{news\u item.published\u at | date(“m/d/Y”)}}
,但随后出现了一个致命错误:
Method "published_at" for object "AppBundle\Entity\News" does not exist in AppBundle:news:index.html.twig at line 7
第7行:
{{news_item.published_|date(“m/d/Y”)}
我还在调试工具栏中得到一个“无效实体”警告,说明如下:
- AppBundle\Entity\Account
- 关联AppBundle\Entity\Account#articles指拥有方字段AppBundle\Entity\News#author,该字段未定义为关联,而是定义为字段
- 关联AppBundle\Entity\Account#articles引用了不存在的拥有方字段AppBundle\Entity\News#author
课堂新闻
{
/**
*@ORM\Column(name=“id”,type=“integer”)
*@ORM\Id
*@ORM\GeneratedValue(strategy=“AUTO”)
*/
私人$id;
/**
*@ORM\Column(name=“title”,type=“string”,length=255)
*/
私人产权;
/**
*@ORM\Column(name=“body”,type=“text”)
*/
私人机构;
/**
*@ORM\Column(name=“author”,type=“integer”)
*@ORM\manytone(targetEntity=“AppBundle\Entity\Account”,inversedBy=“articles”)
*/
私人作家;
/**
*@ORM\Column(name=“published_at”,type=“datetime”)
*/
私人$出版单位:;
}
src/AppBundle/Entity/Repositories/NewsRepository.php
class NewsRepository扩展了EntityRepository
{
/**
*@param$number
*@返回混合
*@throws\Doctrine\ORM\ununiqueresultexception
*/
公共函数findLatest($number){
返回$this->createQueryBuilder('a')
->orderBy('a.published_at','DESC')
->setMaxResults($number)
->getQuery()
->getResult();
}
}
src/AppBundle/Entity/Account.php
类帐户实现AdvancedUserInterface
{
/**
*@ORM\OneToMany(targetEntity=“AppBundle\Entity\News”,mappedBy=“author”)
*/
受保护物品;
}
您的实体类需要getter(和setter)
使用此{{news\u item.published\u at}}
news::getPublished\u at
(…)如果不是,并且如果foo是一个对象,请检查getBar是否是有效的方法;(……)
您还可以使用
{news\u item.getPublished\u at()}}
直接在twig中获取方法。您的实体类需要getter(和setter)
使用此{{news\u item.published\u at}}
news::getPublished\u at
(…)如果不是,并且如果foo是一个对象,请检查getBar是否是有效的方法;(……)
您还可以使用
{{news\u item.getPublished\u at()}
直接获取twig中的方法,因为您使用的是private
属性,您必须在实体中提供public
get/set方法,模板引擎将能够自动访问它们。你在这里复制代码时是添加了你的代码,还是只是把它们漏掉了?@Artamiel:我把它们漏掉是因为我的问题太长了(为了尽量减少垃圾)但是所有的get和setter都是公共的。由于您使用的是private
属性,您必须在实体中提供public
get/set方法,模板引擎将能够自动访问它们。你在这里复制代码时是添加了你的代码,还是只是把它们漏掉了?@Artamiel:我把它们漏掉是因为我的问题太长了(为了尽量减少垃圾)但是所有的get和setter都是公共的。由于您使用的是private
属性,您必须在实体中提供public
get/set方法,模板引擎将能够自动访问它们。你在这里复制代码时是添加了你的代码,还是只是遗漏了它们?@Artamiel:我遗漏了它们是因为我的问题太长(为了尽量减少垃圾),但是所有的get和setter都是公共的。我有get和setter。由于我的问题太长(为了尽量减少垃圾),我把他们排除在这个问题之外。然而,您建议的实体并没有解决我的问题。我用published_at代替publishedat。它确实解决了我的问题。谢谢你,我有接球手和接球手。由于我的问题太长(为了尽量减少垃圾),我把他们排除在这个问题之外。然而,您建议的实体并没有解决我的问题。我用published_at代替publishedat。它确实解决了我的问题。谢谢你,我有接球手和接球手。由于我的问题太长(为了尽量减少垃圾),我把他们排除在这个问题之外。然而,您建议的实体并没有解决我的问题。我用published_at代替publishedat。它确实解决了我的问题。非常感谢。
class News
{
// ...
/**
* @ORM\Column(name="published_at", type="datetime")
*/
private $published_at;
public function getPublished_at()
{
return $this->published_at;
}
}