1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys BlogBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\BlogBundle\Repository
;
14 use Doctrine\ORM\Query\ResultSetMapping
;
16 use Rapsys\BlogBundle\Repository
;
21 * This class was generated by the Doctrine ORM. Add your own custom
22 * repository methods below.
24 class ArticleRepository
extends Repository
{
26 * Find article count as int
28 * @return integer The articles count
30 public function findCountAsInt(): int {
33 SELECT COUNT(at.article_id) AS count
34 FROM RapsysBlogBundle:ArticleTranslation AS at
35 WHERE at.locale = :locale
38 //Get result set mapping instance
39 $req = $this->replace($req);
41 //Get result set mapping instance
42 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
43 $rsm = new ResultSetMapping();
46 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
47 //addScalarResult($sqlColName, $resColName, $type = 'string');
48 $rsm->addScalarResult('count', 'count', 'integer');
52 ->createNativeQuery($req, $rsm)
53 ->getSingleScalarResult();
57 * Find articles as array
59 * @param integer $page The page
60 * @param integer $count The count
61 * @return array The articles array
63 public function findAllAsArray(int $page, int $count): array {
68 GREATEST(a.created, at.created, k.created, kt.created) AS created,
69 GREATEST(a.updated, at.updated, k.updated, kt.updated) AS updated,
70 GREATEST(a.created, at.created, k.created, kt.created, a.updated, at.updated, k.updated, kt.updated) as modified,
75 GROUP_CONCAT(k.id ORDER BY k.id SEPARATOR "\\n") AS k_ids,
76 GROUP_CONCAT(kt.slug ORDER BY k.id SEPARATOR "\\n") AS k_slugs,
77 GROUP_CONCAT(kt.title ORDER BY k.id SEPARATOR "\\n") AS k_titles
78 FROM RapsysBlogBundle:Article AS a
79 JOIN RapsysBlogBundle:ArticleTranslation AS at ON (at.article_id = a.id AND at.locale = :locale)
80 LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak ON (ak.article_id = a.id)
81 LEFT JOIN RapsysBlogBundle:Keyword AS k ON (k.id = ak.keyword_id)
82 LEFT JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = k.id AND kt.locale = :locale)
84 ORDER BY updated, created DESC, a.id
88 //Replace bundle entity name by table name
89 $req = $this->replace($req);
91 //Get result set mapping instance
92 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
93 $rsm = new ResultSetMapping();
96 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
97 //addScalarResult($sqlColName, $resColName, $type = 'string');
98 $rsm->addScalarResult('id', 'id', 'integer')
99 ->addScalarResult('body', 'body', 'string')
100 ->addScalarResult('description', 'description', 'string')
101 ->addScalarResult('slug', 'slug', 'string')
102 ->addScalarResult('title', 'title', 'string')
103 ->addScalarResult('k_ids', 'k_ids', 'string')
104 ->addScalarResult('k_slugs', 'k_slugs', 'string')
105 ->addScalarResult('k_titles', 'k_titles', 'string')
106 ->addScalarResult('created', 'created', 'datetime_immutable')
107 ->addScalarResult('updated', 'updated', 'datetime_immutable')
108 ->addScalarResult('modified', 'modified', 'datetime_immutable')
109 ->addIndexByScalar('id');
112 $result = $this->manager
113 ->createNativeQuery($req, $rsm)
114 ->setParameter('offset', $page * $count)
115 ->setParameter('count', $count)
121 //Iterate on each city
122 foreach($result as $data) {
124 $return[$data['id']] = [
125 'id' => $id = $data['id'],
126 'body' => $data['body'],
127 'description' => $data['description'],
128 'slug' => $slug = $data['slug'],
129 'title' => $data['title'],
130 'created' => $data['created'],
131 'updated' => $data['updated'],
132 'modified' => $data['modified'],
133 'link' => $this->router
->generate('rapsysblog_article_view', ['id' => $id, 'slug' => $slug]),
137 //Explode keyword ids
138 $data['k_ids'] = explode("\n", $data['k_ids']);
140 //Explode keyword slugs
141 $data['k_slugs'] = explode("\n", $data['k_slugs']);
143 //Explode keyword titles
144 $data['k_titles'] = explode("\n", $data['k_titles']);
146 foreach($data['k_ids'] as $k => $id) {
147 $return[$data['id']]['keywords'][$id] = [
149 'slug' => $slug = $data['k_slugs'][$k],
150 'title' => $data['k_titles'][$k],
151 'link' => $this->router
->generate('rapsysblog_keyword_view', ['id' => $id, 'slug' => $slug]),
161 * Find article by id as array
163 * @param integer $id The article id
164 * @return array The article array
166 public function findByIdAsArray(int $id): array {
171 GREATEST(b.created, kat.created) AS created,
172 GREATEST(b.updated, kat.updated) AS updated,
173 GREATEST(b.created, b.updated, kat.created, kat.updated) AS modified,
181 GROUP_CONCAT(kat.locale ORDER BY kat.locale SEPARATOR "\\n") AS kat_locales,
182 GROUP_CONCAT(kat.slug ORDER BY kat.locale SEPARATOR "\\n") AS kat_slugs,
183 GROUP_CONCAT(kat.title ORDER BY kat.locale SEPARATOR "\\n") AS kat_titles
187 GREATEST(a.created, at.created, k.created, kt.created) AS created,
188 GREATEST(a.updated, at.updated, k.updated, kt.updated) AS updated,
193 GROUP_CONCAT(k.id ORDER BY k.id SEPARATOR "\\n") AS k_ids,
194 GROUP_CONCAT(kt.slug ORDER BY k.id SEPARATOR "\\n") AS k_slugs,
195 GROUP_CONCAT(kt.title ORDER BY k.id SEPARATOR "\\n") AS k_titles
196 FROM RapsysBlogBundle:Article AS a
197 JOIN RapsysBlogBundle:ArticleTranslation AS at ON (at.article_id = a.id AND at.locale = :locale)
198 LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak ON (ak.article_id = a.id)
199 LEFT JOIN RapsysBlogBundle:Keyword AS k ON (k.id = ak.keyword_id)
200 LEFT JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = k.id AND kt.locale = :locale)
206 LEFT JOIN RapsysBlogBundle:ArticleTranslation AS kat ON (kat.article_id = b.id AND kat.locale <> :locale)
210 //Replace bundle entity name by table name
211 $req = $this->replace($req);
213 //Get result set mapping instance
214 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
215 $rsm = new ResultSetMapping();
218 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
219 //addScalarResult($sqlColName, $resColName, $type = 'string');
220 $rsm->addScalarResult('id', 'id', 'integer')
221 ->addScalarResult('body', 'body', 'string')
222 ->addScalarResult('description', 'description', 'string')
223 ->addScalarResult('slug', 'slug', 'string')
224 ->addScalarResult('title', 'title', 'string')
225 ->addScalarResult('k_ids', 'k_ids', 'string')
226 ->addScalarResult('k_slugs', 'k_slugs', 'string')
227 ->addScalarResult('k_titles', 'k_titles', 'string')
228 ->addScalarResult('kat_locales', 'kat_locales', 'string')
229 ->addScalarResult('kat_slugs', 'kat_slugs', 'string')
230 ->addScalarResult('kat_titles', 'kat_titles', 'string')
231 ->addScalarResult('created', 'created', 'datetime_immutable')
232 ->addScalarResult('updated', 'updated', 'datetime_immutable')
233 ->addScalarResult('modified', 'modified', 'datetime_immutable');
236 $data = $this->manager
237 ->createNativeQuery($req, $rsm)
238 ->setParameter('id', $id)
239 ->getOneOrNullResult();
243 'id' => $id = $data['id'],
244 'body' => $data['body'],
245 'description' => $data['description'],
246 'slug' => $slug = $data['slug'],
247 'title' => $data['title'],
248 'created' => $data['created'],
249 'updated' => $data['updated'],
250 'modified' => $data['modified'],
251 'link' => $this->router
->generate('rapsysblog_article_view', ['id' => $id, 'slug' => $slug]),
256 //Explode keyword ids
257 $data['k_ids'] = explode("\n", $data['k_ids']);
259 //Explode keyword slugs
260 $data['k_slugs'] = explode("\n", $data['k_slugs']);
262 //Explode keyword titles
263 $data['k_titles'] = explode("\n", $data['k_titles']);
265 foreach($data['k_ids'] as $k => $kid) {
266 $return['keywords'][$kid] = [
268 'slug' => $slug = $data['k_slugs'][$k],
269 'title' => $data['k_titles'][$k],
270 'link' => $this->router
->generate('rapsysblog_keyword_view', ['id' => $kid, 'slug' => $slug]),
274 //Explode alternate locales
275 $data['kat_locales'] = explode("\n", $data['kat_locales']);
277 //Explode alternate slugs
278 $data['kat_slugs'] = explode("\n", $data['kat_slugs']);
280 //Explode alternate titles
281 $data['kat_titles'] = explode("\n", $data['kat_titles']);
283 foreach($data['kat_locales'] as $k => $locale) {
284 $return['alternates'][$locale] = [
286 'slug' => $slug = $data['kat_slugs'][$k],
287 'title' => $data['kat_titles'][$k],
288 'link' => $this->router
->generate('rapsysblog_article_view', ['_locale' => $locale, 'id' => $id, 'slug' => $slug]),