一个接一个的学说和日期

Doctrine OneByOne's and Date

我正在使用 Symfony 2.7.1,但在使用我的新闻实体时似乎遇到了问题。我正在尝试在我的树枝模板中使用 published_at。

我尝试使用 {{ news_item.published_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_at | date("m/d/Y") }}

我还在调试工具栏中收到一条 'Invalid entities' 警告,说明以下内容;

这些是我的文件。我希望有人能帮助我把我推向正确的方向:

src/AppBundle/Entity/News.php

class News
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @ORM\Column(name="body", type="text")
     */
    private $body;

    /**
     * @ORM\Column(name="author", type="integer")
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Account", inversedBy="articles")
     */
    private $author;

    /**
     * @ORM\Column(name="published_at", type="datetime")
     */
    private $published_at;
}

src/AppBundle/Entity/Repositories/NewsRepository.php

class NewsRepository extends EntityRepository
{
    /**
     * @param $number
     * @return mixed
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function findLatest($number) {
        return $this->createQueryBuilder('a')
            ->orderBy('a.published_at', 'DESC')
            ->setMaxResults($number)
            ->getQuery()
            ->getResult();
    }
}

src/AppBundle/Entity/Account.php

class Account implements AdvancedUserInterface
{
    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\News", mappedBy="author")
     */
    protected $articles;
}

您的实体 class 需要吸气剂(和 setter)。

class News
{
    // ...

    /**
     * @ORM\Column(name="published_at", type="datetime")
     */
    private $published_at;

    public function getPublished_at()
    {
        return $this->published_at;
    }
}

用这个 {{ news_item.published_at }} will call News::getPublished_at.

(...) if not, and if foo is an object, check that getBar is a valid method; (...)

你也可以直接在twig中获取方法{{ news_item.getPublished_at() }}