<?php
namespace App\Repository;
use App\Entity\Blog;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Blog|null find($id, $lockMode = null, $lockVersion = null)
* @method Blog|null findOneBy(array $criteria, array $orderBy = null)
* @method Blog[] findAll()
* @method Blog[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class BlogRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Blog::class);
}
public function listBlog($desde, $hasta, $categoria, $total=false, $client_id) {
$conn = $this->getEntityManager()->getConnection();
$query = " SELECT b.blog_id as blogId,
b.title,
b.cover_path as coverPath,
b.content,
DATE_FORMAT(b.created_at, '%M %d, %Y') as createdAt,
bc.name,
b.short_content as shortContent,
bc.name AS categoryName
FROM blog b
LEFT JOIN blog_category bc ON bc.blog_category_id = b.blog_category_id
WHERE b.is_published = 1 and b.is_active = 1 AND b.client_id = '$client_id'
";
if($categoria != 'all'){
$query= $query.=" AND b.blog_category_id = '$categoria'";
}
if($total == false){
$query = $query.="LIMIT $desde, $hasta";
}
return $conn->fetchAllAssociative($query);
/*$res = $this->getEntityManager()->getConnection()->prepare($query);
$res->execute();
return $res->fetchAll();*/
}
public function getCategoriesBlog($slug) {
$conn = $this->getEntityManager()->getConnection();
$query = " SELECT
bc.*,
(SELECT COUNT(*) FROM blog WHERE blog.blog_category_id = bc.blog_category_id AND blog.is_active = 1 AND blog.is_published = 1) AS count_blogs
FROM blog_category bc, client c
WHERE bc.is_active = 1
AND c.client_id = bc.client_id
AND c.slug = '$slug'
HAVING count_blogs > 0
";
return $conn->fetchAllAssociative($query);
/*$res = $this->getEntityManager()->getConnection()->prepare($query);
$res->execute();
return $res->fetchAll();*/
}
public function getBlogsGrouped() {
$conn = $this->getEntityManager()->getConnection();
$query = " SELECT
count(blog_id) as count,
month(created_at) as month_number,
year(created_at) as year
FROM blog
WHERE is_active = 1 and is_published = 1
GROUP BY month(created_at), year(created_at) ";
return $conn->fetchAllAssociative($query);
/*
$res = $this->getEntityManager()->getConnection()->prepare($query);
$res->execute();
return $res->fetchAll();*/
}
}