php 从存储库提取分支函数

wtzytmuj  于 2023-05-05  发布在  PHP
关注(0)|答案(1)|浏览(156)

我试图拉一个完整的用户列表进入一个表。
我有一个userStatistics的函数,我假设它允许我从DB输出所有用户行。

public function userStatistics(){

        $stmtParams = array(
            'status' => 'Confirm'
        );

        $andWhere = ' WHERE  u.`created_at` <= NOW() - INTERVAL 7 DAY';
        $query = $this->getEntityManager()->getConnection()->prepare("
         SELECT 
                COUNT(u.`id`) AS regCount,
                DATE_FORMAT(DATE(b.Days), '%a' ) AS regDate
            FROM 
                (SELECT a.Days 
                FROM (
                    SELECT 
                        curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS Days
                    FROM   (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
                    CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
                    CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
                ) a
                WHERE a.Days >= curdate() - INTERVAL 6 DAY) b 
                LEFT JOIN `user` u ON DATE_FORMAT(u.`created_at`, '%Y-%m-%d') = b.Days
                GROUP BY b.Days
                ORDER BY b.Days
        ");


        $query->execute($stmtParams);
        $result = $query->fetchAll();

        return !empty($result) ? $result : array();
        
    }

目前正在尝试与下面类似的格式。

{{%for user.userStatistics('$result') %}}
  
  {{ user.id }}
  
  {% endfor %}
{{%for user.userStatistics('$result') %}}
  
  {{ user.id }}
  
  {% endfor %}
{{ user.userStatistics }}
  
  {{ user.id }}

两者都失败,出现服务器错误。

6rqinv9w

6rqinv9w1#

我猜你的函数在你的仓库类中,所以你可以创建一个twig扩展来调用你的仓库函数,就像这样

// src/Twig/UserExtension.php
<?php

namespace App\Twig;

use App\Repository\UserRepository;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class UserExtension extends AbstractExtension
{
    private $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function getFilters(): array
    {
        return [];
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('getUserStatistic', [$this, 'getStatistic']),
        ];
    }

    public function getStatistic()
    {
        return $this->userRepository->userStatistics();
    }
}

然后用小树枝把它扔下去

{% for item in getUserStatistic() %}
    ...
    {{ item.id }}
    ...
{% endfor %}

相关问题