]> Raphaël G. Git Repositories - blogbundle/blob - Repository/KeywordRepository.php
Replace EntityRepository by Repository class
[blogbundle] / Repository / KeywordRepository.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys BlogBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\BlogBundle\Repository;
13
14 use Doctrine\ORM\Query\ResultSetMapping;
15
16 use Rapsys\BlogBundle\Repository;
17
18 /**
19 * KeywordRepository
20 *
21 * This class was generated by the Doctrine ORM. Add your own custom
22 * repository methods below.
23 */
24 class KeywordRepository extends Repository {
25 /**
26 * Find keyword count as int
27 *
28 * @return integer The keywords count
29 */
30 public function findCountAsInt(): int {
31 //Set the request
32 $req = <<<SQL
33 SELECT COUNT(kt.keyword_id) AS count
34 FROM RapsysBlogBundle:KeywordTranslation AS kt
35 WHERE kt.locale = :locale
36 SQL;
37
38 //Get result set mapping instance
39 $req = $this->replace($req);
40
41 //Get result set mapping instance
42 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
43 $rsm = new ResultSetMapping();
44
45 //Declare all fields
46 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
47 //addScalarResult($sqlColName, $resColName, $type = 'string');
48 $rsm->addScalarResult('count', 'count', 'integer');
49
50 //Get result
51 return $this->manager
52 ->createNativeQuery($req, $rsm)
53 ->getSingleScalarResult();
54 }
55
56 /**
57 * Find keywords as array
58 *
59 * @param integer $page The page
60 * @param integer $count The count
61 * @return array The keywords array
62 */
63 public function findAllAsArray(int $page, int $count): array {
64 //Set the request
65 $req = <<<SQL
66 SELECT
67 k.id,
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,
71 kt.description,
72 kt.slug,
73 kt.title
74 FROM RapsysBlogBundle:Keyword AS k
75 JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = k.id AND kt.locale = :locale)
76 GROUP BY k.id
77 ORDER BY updated, created DESC, k.id
78 LIMIT :offset, :count
79 SQL;
80
81 //Replace bundle entity name by table name
82 $req = $this->replace($req);
83
84 //Get result set mapping instance
85 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
86 $rsm = new ResultSetMapping();
87
88 //Declare all fields
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');
99
100 //Get result
101 $result = $this->manager
102 ->createNativeQuery($req, $rsm)
103 ->setParameter('offset', $page * $count)
104 ->setParameter('count', $count)
105 ->getArrayResult();
106
107 //Set return
108 $return = [];
109
110 //Iterate on each city
111 foreach($result as $data) {
112 //Add to return
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])
122 ];
123 }
124
125 //Return return
126 return $return;
127 }
128
129 /**
130 * Find keyword by id as array
131 *
132 * @param integer $id The keyword id
133 * @return array The keyword array
134 */
135 public function findByIdAsArray(int $id): array {
136 //Set the request
137 $req = <<<SQL
138 SELECT
139 c.id,
140 c.created,
141 c.updated,
142 c.modified,
143 c.description,
144 c.slug,
145 c.title,
146 c.a_ids,
147 c.a_descriptions,
148 c.a_slugs,
149 c.a_titles,
150 c.ak_aids,
151 c.ak_kids,
152 c.ak_slugs,
153 c.ak_titles,
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
157 FROM (
158 SELECT
159 b.id,
160 b.created,
161 b.updated,
162 GREATEST(b.modified, k.created, k.updated) AS modified,
163 b.description,
164 b.slug,
165 b.title,
166 b.a_ids,
167 b.a_descriptions,
168 b.a_slugs,
169 b.a_titles,
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
174 FROM (
175 SELECT
176 k.id,
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,
180 kt.description,
181 kt.slug,
182 kt.title,
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)
192 WHERE k.id = :id
193 GROUP BY k.id
194 ORDER BY NULL
195 LIMIT 0, :limit
196 ) AS b
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)
201 GROUP BY b.id
202 ORDER BY NULL
203 LIMIT 0, :limit
204 ) AS c
205 LEFT JOIN RapsysBlogBundle:KeywordTranslation AS ckt ON (ckt.keyword_id = c.id AND ckt.locale <> :locale)
206 GROUP BY c.id
207 SQL;
208
209 //Replace bundle entity name by table name
210 $req = $this->replace($req);
211
212 //Get result set mapping instance
213 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
214 $rsm = new ResultSetMapping();
215
216 //Declare all fields
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');
238
239 //Get data
240 $data = $this->manager
241 ->createNativeQuery($req, $rsm)
242 ->setParameter('id', $id)
243 ->getOneOrNullResult();
244
245 //Set return
246 $return = [
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]),
255 'alternates' => [],
256 'articles' => []
257 ];
258
259 //Explode article ids
260 $data['a_ids'] = explode("\n", $data['a_ids']);
261
262 //Explode article descriptions
263 $data['a_descriptions'] = explode("\n", $data['a_descriptions']);
264
265 //Explode article slugs
266 $data['a_slugs'] = explode("\n", $data['a_slugs']);
267
268 //Explode article titles
269 $data['a_titles'] = explode("\n", $data['a_titles']);
270
271 //Iterate on articles
272 foreach($data['a_ids'] as $k => $aid) {
273 $return['articles'][$aid] = [
274 'id' => $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]),
279 'keywords' => []
280 ];
281 }
282
283 //Explode article ids
284 $data['ak_aids'] = explode("\n", $data['ak_aids']);
285
286 //Explode article keyword ids
287 $data['ak_kids'] = explode("\n", $data['ak_kids']);
288
289 //Explode article keyword slugs
290 $data['ak_slugs'] = explode("\n", $data['ak_slugs']);
291
292 //Explode article keyword titles
293 $data['ak_titles'] = explode("\n", $data['ak_titles']);
294
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]),
302 ];
303 }
304
305 //Explode alternate locales
306 $data['ckt_locales'] = explode("\n", $data['ckt_locales']);
307
308 //Explode alternate slugs
309 $data['ckt_slugs'] = explode("\n", $data['ckt_slugs']);
310
311 //Explode alternate titles
312 $data['ckt_titles'] = explode("\n", $data['ckt_titles']);
313
314 foreach($data['ckt_locales'] as $k => $locale) {
315 $return['alternates'][$locale] = [
316 'locale' => $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]),
320 ];
321 }
322
323 //Return return
324 return $return;
325 }
326 }