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 KeywordRepository
extends Repository
{
26 * Find keyword count as int
28 * @return integer The keywords count
30 public function findCountAsInt(): int {
33 SELECT COUNT(kt.keyword_id) AS count
34 FROM RapsysBlogBundle:KeywordTranslation AS kt
35 WHERE kt.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 keywords as array
59 * @param integer $page The page
60 * @param integer $count The count
61 * @return array The keywords array
63 public function findAllAsArray(int $page, int $count): array {
68 GREATEST(k.created, kt.created) AS created,
69 GREATEST(k.updated, kt.updated) AS updated,
70 GREATEST(k.created, kt.created, k.updated, kt.updated) AS modified,
74 FROM RapsysBlogBundle:Keyword AS k
75 JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = k.id AND kt.locale = :locale)
77 ORDER BY updated, created DESC, k.id
81 //Replace bundle entity name by table name
82 $req = $this->replace($req);
84 //Get result set mapping instance
85 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
86 $rsm = new ResultSetMapping();
89 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
90 //addScalarResult($sqlColName, $resColName, $type = 'string');
91 $rsm->addScalarResult('id', 'id', 'integer')
92 ->addScalarResult('description', 'description', 'string')
93 ->addScalarResult('slug', 'slug', 'string')
94 ->addScalarResult('title', 'title', 'string')
95 ->addScalarResult('created', 'created', 'datetime_immutable')
96 ->addScalarResult('updated', 'updated', 'datetime_immutable')
97 ->addScalarResult('modified', 'modified', 'datetime_immutable')
98 ->addIndexByScalar('id');
101 $result = $this->manager
102 ->createNativeQuery($req, $rsm)
103 ->setParameter('offset', $page * $count)
104 ->setParameter('count', $count)
110 //Iterate on each city
111 foreach($result as $data) {
113 $return[$data['id']] = [
114 'id' => $id = $data['id'],
115 'description' => $data['description'],
116 'slug' => $slug = $data['slug'],
117 'title' => $data['title'],
118 'created' => $data['created'],
119 'updated' => $data['updated'],
120 'modified' => $data['modified'],
121 'link' => $this->router
->generate('rapsysblog_keyword_view', ['id' => $id, 'slug' => $slug])
130 * Find keyword by id as array
132 * @param integer $id The keyword id
133 * @return array The keyword array
135 public function findByIdAsArray(int $id): array {
154 GROUP_CONCAT(ckt.locale ORDER BY ckt.locale SEPARATOR "\\n") AS ckt_locales,
155 GROUP_CONCAT(ckt.slug ORDER BY ckt.locale SEPARATOR "\\n") AS ckt_slugs,
156 GROUP_CONCAT(ckt.title ORDER BY ckt.locale SEPARATOR "\\n") AS ckt_titles
162 GREATEST(b.modified, k.created, k.updated) AS modified,
170 GROUP_CONCAT(ak2.article_id ORDER BY ak2.article_id, ak2.keyword_id SEPARATOR "\\n") AS ak_aids,
171 GROUP_CONCAT(ak2.keyword_id ORDER BY ak2.article_id, ak2.keyword_id SEPARATOR "\\n") AS ak_kids,
172 GROUP_CONCAT(kt.slug ORDER BY ak2.article_id, ak2.keyword_id SEPARATOR "\\n") AS ak_slugs,
173 GROUP_CONCAT(kt.title ORDER BY ak2.article_id, ak2.keyword_id SEPARATOR "\\n") AS ak_titles
177 GREATEST(k.created, kt.created) AS created,
178 GREATEST(k.updated, kt.updated) AS updated,
179 GREATEST(k.created, kt.created, a.created, at.created, k.updated, kt.updated, a.updated, at.updated) AS modified,
183 GROUP_CONCAT(a.id ORDER BY a.id SEPARATOR "\\n") AS a_ids,
184 GROUP_CONCAT(at.description ORDER BY a.id SEPARATOR "\\n") AS a_descriptions,
185 GROUP_CONCAT(at.slug ORDER BY a.id SEPARATOR "\\n") AS a_slugs,
186 GROUP_CONCAT(at.title ORDER BY a.id SEPARATOR "\\n") AS a_titles
187 FROM RapsysBlogBundle:Keyword AS k
188 JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = k.id AND kt.locale = :locale)
189 LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak ON (ak.keyword_id = k.id)
190 LEFT JOIN RapsysBlogBundle:Article AS a ON (a.id = ak.article_id)
191 LEFT JOIN RapsysBlogBundle:ArticleTranslation AS at ON (at.article_id = a.id AND at.locale = :locale)
197 LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak ON (ak.keyword_id = b.id)
198 LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak2 ON (ak2.article_id = ak.article_id)
199 LEFT JOIN RapsysBlogBundle:Keyword AS k ON (k.id = ak2.keyword_id)
200 LEFT JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = ak2.keyword_id AND kt.locale = :locale)
205 LEFT JOIN RapsysBlogBundle:KeywordTranslation AS ckt ON (ckt.keyword_id = c.id AND ckt.locale <> :locale)
209 //Replace bundle entity name by table name
210 $req = $this->replace($req);
212 //Get result set mapping instance
213 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
214 $rsm = new ResultSetMapping();
217 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
218 //addScalarResult($sqlColName, $resColName, $type = 'string');
219 $rsm->addScalarResult('id', 'id', 'integer')
220 ->addScalarResult('body', 'body', 'string')
221 ->addScalarResult('description', 'description', 'string')
222 ->addScalarResult('slug', 'slug', 'string')
223 ->addScalarResult('title', 'title', 'string')
224 ->addScalarResult('a_ids', 'a_ids', 'string')
225 ->addScalarResult('a_descriptions', 'a_descriptions', 'string')
226 ->addScalarResult('a_slugs', 'a_slugs', 'string')
227 ->addScalarResult('a_titles', 'a_titles', 'string')
228 ->addScalarResult('ak_aids', 'ak_aids', 'string')
229 ->addScalarResult('ak_kids', 'ak_kids', 'string')
230 ->addScalarResult('ak_slugs', 'ak_slugs', 'string')
231 ->addScalarResult('ak_titles', 'ak_titles', 'string')
232 ->addScalarResult('ckt_locales', 'ckt_locales', 'string')
233 ->addScalarResult('ckt_slugs', 'ckt_slugs', 'string')
234 ->addScalarResult('ckt_titles', 'ckt_titles', 'string')
235 ->addScalarResult('created', 'created', 'datetime_immutable')
236 ->addScalarResult('updated', 'updated', 'datetime_immutable')
237 ->addScalarResult('modified', 'modified', 'datetime_immutable');
240 $data = $this->manager
241 ->createNativeQuery($req, $rsm)
242 ->setParameter('id', $id)
243 ->getOneOrNullResult();
247 'id' => $id = $data['id'],
248 'description' => $data['description'],
249 'slug' => $slug = $data['slug'],
250 'title' => $data['title'],
251 'created' => $data['created'],
252 'updated' => $data['updated'],
253 'modified' => $data['modified'],
254 'link' => $this->router
->generate('rapsysblog_keyword_view', ['id' => $id, 'slug' => $slug]),
259 //Explode article ids
260 $data['a_ids'] = explode("\n", $data['a_ids']);
262 //Explode article descriptions
263 $data['a_descriptions'] = explode("\n", $data['a_descriptions']);
265 //Explode article slugs
266 $data['a_slugs'] = explode("\n", $data['a_slugs']);
268 //Explode article titles
269 $data['a_titles'] = explode("\n", $data['a_titles']);
271 //Iterate on articles
272 foreach($data['a_ids'] as $k => $aid) {
273 $return['articles'][$aid] = [
275 'description' => $data['a_descriptions'][$k],
276 'slug' => $slug = $data['a_slugs'][$k],
277 'title' => $data['a_titles'][$k],
278 'link' => $this->router
->generate('rapsysblog_article_view', ['id' => $aid, 'slug' => $slug]),
283 //Explode article ids
284 $data['ak_aids'] = explode("\n", $data['ak_aids']);
286 //Explode article keyword ids
287 $data['ak_kids'] = explode("\n", $data['ak_kids']);
289 //Explode article keyword slugs
290 $data['ak_slugs'] = explode("\n", $data['ak_slugs']);
292 //Explode article keyword titles
293 $data['ak_titles'] = explode("\n", $data['ak_titles']);
295 //Iterate on articles
296 foreach($data['ak_aids'] as $k => $aid) {
297 $return['articles'][$aid]['keywords'][$data['ak_kids'][$k]] = [
298 'id' => $data['ak_kids'][$k],
299 'slug' => $slug = $data['ak_slugs'][$k],
300 'title' => $data['ak_titles'][$k],
301 'link' => $this->router
->generate('rapsysblog_keyword_view', ['id' => $data['ak_kids'][$k], 'slug' => $slug]),
305 //Explode alternate locales
306 $data['ckt_locales'] = explode("\n", $data['ckt_locales']);
308 //Explode alternate slugs
309 $data['ckt_slugs'] = explode("\n", $data['ckt_slugs']);
311 //Explode alternate titles
312 $data['ckt_titles'] = explode("\n", $data['ckt_titles']);
314 foreach($data['ckt_locales'] as $k => $locale) {
315 $return['alternates'][$locale] = [
317 'slug' => $slug = $data['ckt_slugs'][$k],
318 'title' => $data['ckt_titles'][$k],
319 'link' => $this->router
->generate('rapsysblog_keyword_view', ['_locale' => $locale, 'id' => $id, 'slug' => $slug]),