Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2-细枝扩展错误无法访问空属性_Php_Symfony_Twig - Fatal编程技术网

Php Symfony2-细枝扩展错误无法访问空属性

Php Symfony2-细枝扩展错误无法访问空属性,php,symfony,twig,Php,Symfony,Twig,刚开始使用细枝扩展的人,可以在这方面使用一些帮助,但不能找出问题所在 除了我的扩展类中的函数调用之外,所有东西似乎都加载正确 我收到以下错误: FatalErrorException:错误:无法访问/./PostExtension.php第32行中的属性 第32行: 公共函数getPostCount($year,$month) 已经搜索解决方案和阅读文档好几个小时了,但还没有找到解决方案。有什么帮助吗? PostExtension.php class PostExtension extends

刚开始使用细枝扩展的人,可以在这方面使用一些帮助,但不能找出问题所在

除了我的扩展类中的函数调用之外,所有东西似乎都加载正确

我收到以下错误:

FatalErrorException:错误:无法访问/./PostExtension.php第32行中的属性

第32行:

公共函数getPostCount($year,$month)

已经搜索解决方案和阅读文档好几个小时了,但还没有找到解决方案。有什么帮助吗?

PostExtension.php

class PostExtension extends \Twig_Extension
{
private $em;

public function __construct(EntityManager $em)
{
    $this->em = $em;
}

public function getFilters()
{
    return array(
    );
}

public function getFunctions()
{
    return array(
        'getPostCount' => new \Twig_Function_Method($this, 'getPostCount')
    );
}

public function getPostCount($year, $month)
{
    $post = $this->$em->getRepository('AcmeDemoBundle:Post')
        ->getPostCountsByMonth($year, $month);

    return $post;
}

public function getName()
{
    return 'post_extension';
}
}
小树枝

{{ getPostCount('2014', 'July') }}
services.yml

services:
    acme.twig.extension:
        class: Acmer\DemoBundle\Twig\PostExtension
        tags:
            - { name: twig.extension }
        arguments:
            em: "@doctrine.orm.entity_manager"
存储库-getPostCountsByMonth

public function getPostCountsByMonth($year, $month)
{
    // Query for blog posts in each month
    $date = new \DateTime("{$year}-{$month}-01");
    $toDate = clone $date;
    $toDate->modify("next month midnight -1 second");

    $query = $this->createQueryBuilder('post')
        ->where('post.created BETWEEN :start AND :end')
        ->addOrderBy('post.created', 'DESC')
        ->setParameter('start', $date)
        ->setParameter('end', $toDate);

    $query->select('COUNT(post.created)');

    $month = $query
        ->getQuery()
        ->getSingleScalarResult();

    return $month;
}

您的
getPostCount()
方法中有一个输入错误

在这一行
$this->$em
您应该删除第二个$符号,因为您想访问
em
属性,所以您应该像这样使用它:

$post = $this->em->getRepository('AcmeDemoBundle:Post')
    ->getPostCountsByMonth($year, $month);

您的
getPostCount()
方法中有一个输入错误

在这一行
$this->$em
您应该删除第二个$符号,因为您想访问
em
属性,所以您应该像这样使用它:

$post = $this->em->getRepository('AcmeDemoBundle:Post')
    ->getPostCountsByMonth($year, $month);

您的
getPostCount()
方法中有一个输入错误

在这一行
$this->$em
您应该删除第二个$符号,因为您想访问
em
属性,所以您应该像这样使用它:

$post = $this->em->getRepository('AcmeDemoBundle:Post')
    ->getPostCountsByMonth($year, $month);

您的
getPostCount()
方法中有一个输入错误

在这一行
$this->$em
您应该删除第二个$符号,因为您想访问
em
属性,所以您应该像这样使用它:

$post = $this->em->getRepository('AcmeDemoBundle:Post')
    ->getPostCountsByMonth($year, $month);

您的getPostCount方法中有一个输入错误
$this->$em
您应该删除$符号,因为您想访问
em
属性,所以请使用
$this->em->…
完美!!!真不敢相信我没看到。非常感谢你!请回答这个问题,我很乐意接受,先生!您的getPostCount方法中有一个输入错误
$this->$em
您应该删除$符号,因为您想访问
em
属性,所以请使用
$this->em->…
完美!!!真不敢相信我没看到。非常感谢你!请回答这个问题,我很乐意接受,先生!您的getPostCount方法中有一个输入错误
$this->$em
您应该删除$符号,因为您想访问
em
属性,所以请使用
$this->em->…
完美!!!真不敢相信我没看到。非常感谢你!请回答这个问题,我很乐意接受,先生!您的getPostCount方法中有一个输入错误
$this->$em
您应该删除$符号,因为您想访问
em
属性,所以请使用
$this->em->…
完美!!!真不敢相信我没看到。非常感谢你!请回答这个问题,我很乐意接受,先生!