代码之家  ›  专栏  ›  技术社区  ›  Jim white

Twig函数从存储库中提取

  •  0
  • Jim white  · 技术社区  · 3 年前

    我正在尝试拉一个完整的用户列表输入到一个表中。

    我有一个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 }}
    

    两者都因服务器错误而失败。

    0 回复  |  直到 3 年前
        1
  •  0
  •   Fatal Error    3 年前

    我想您的函数在存储库类中,所以您可以创建一个trick扩展来调用您的repo函数,如下所示

    // 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 %}