]> Raphaël G. Git Repositories - blogbundle/blob - Repository/ArticleRepository.php
Replace EntityRepository by Repository class
[blogbundle] / Repository / ArticleRepository.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 * ArticleRepository
20 *
21 * This class was generated by the Doctrine ORM. Add your own custom
22 * repository methods below.
23 */
24 class ArticleRepository extends Repository {
25 /**
26 * Find article count as int
27 *
28 * @return integer The articles count
29 */
30 public function findCountAsInt(): int {
31 //Set the request
32 $req = <<<SQL
33 SELECT COUNT(at.article_id) AS count
34 FROM RapsysBlogBundle:ArticleTranslation AS at
35 WHERE at.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 articles as array
58 *
59 * @param integer $page The page
60 * @param integer $count The count
61 * @return array The articles array
62 */
63 public function findAllAsArray(int $page, int $count): array {
64 //Set the request
65 $req = <<<SQL
66 SELECT
67 a.id,
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,
71 at.body,
72 at.description,
73 at.slug,
74 at.title,
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)
83 GROUP BY a.id
84 ORDER BY updated, created DESC, a.id
85 LIMIT :offset, :count
86 SQL;
87
88 //Replace bundle entity name by table name
89 $req = $this->replace($req);
90
91 //Get result set mapping instance
92 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
93 $rsm = new ResultSetMapping();
94
95 //Declare all fields
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');
110
111 //Get result
112 $result = $this->manager
113 ->createNativeQuery($req, $rsm)
114 ->setParameter('offset', $page * $count)
115 ->setParameter('count', $count)
116 ->getArrayResult();
117
118 //Set return
119 $return = [];
120
121 //Iterate on each city
122 foreach($result as $data) {
123 //Add to return
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]),
134 'keywords' => []
135 ];
136
137 //Explode keyword ids
138 $data['k_ids'] = explode("\n", $data['k_ids']);
139
140 //Explode keyword slugs
141 $data['k_slugs'] = explode("\n", $data['k_slugs']);
142
143 //Explode keyword titles
144 $data['k_titles'] = explode("\n", $data['k_titles']);
145
146 foreach($data['k_ids'] as $k => $id) {
147 $return[$data['id']]['keywords'][$id] = [
148 'id' => $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]),
152 ];
153 }
154 }
155
156 //Return return
157 return $return;
158 }
159
160 /**
161 * Find article by id as array
162 *
163 * @param integer $id The article id
164 * @return array The article array
165 */
166 public function findByIdAsArray(int $id): array {
167 //Set the request
168 $req = <<<SQL
169 SELECT
170 b.id,
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,
174 b.body,
175 b.description,
176 b.slug,
177 b.title,
178 b.k_ids,
179 b.k_slugs,
180 b.k_titles,
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
184 FROM (
185 SELECT
186 a.id,
187 GREATEST(a.created, at.created, k.created, kt.created) AS created,
188 GREATEST(a.updated, at.updated, k.updated, kt.updated) AS updated,
189 at.body,
190 at.description,
191 at.slug,
192 at.title,
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)
201 WHERE a.id = :id
202 GROUP BY a.id
203 ORDER BY NULL
204 LIMIT 0, :limit
205 ) AS b
206 LEFT JOIN RapsysBlogBundle:ArticleTranslation AS kat ON (kat.article_id = b.id AND kat.locale <> :locale)
207 GROUP BY b.id
208 SQL;
209
210 //Replace bundle entity name by table name
211 $req = $this->replace($req);
212
213 //Get result set mapping instance
214 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
215 $rsm = new ResultSetMapping();
216
217 //Declare all fields
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');
234
235 //Get data
236 $data = $this->manager
237 ->createNativeQuery($req, $rsm)
238 ->setParameter('id', $id)
239 ->getOneOrNullResult();
240
241 //Set return
242 $return = [
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]),
252 'alternates' => [],
253 'keywords' => []
254 ];
255
256 //Explode keyword ids
257 $data['k_ids'] = explode("\n", $data['k_ids']);
258
259 //Explode keyword slugs
260 $data['k_slugs'] = explode("\n", $data['k_slugs']);
261
262 //Explode keyword titles
263 $data['k_titles'] = explode("\n", $data['k_titles']);
264
265 foreach($data['k_ids'] as $k => $kid) {
266 $return['keywords'][$kid] = [
267 'id' => $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]),
271 ];
272 }
273
274 //Explode alternate locales
275 $data['kat_locales'] = explode("\n", $data['kat_locales']);
276
277 //Explode alternate slugs
278 $data['kat_slugs'] = explode("\n", $data['kat_slugs']);
279
280 //Explode alternate titles
281 $data['kat_titles'] = explode("\n", $data['kat_titles']);
282
283 foreach($data['kat_locales'] as $k => $locale) {
284 $return['alternates'][$locale] = [
285 'locale' => $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]),
289 ];
290 }
291
292 //Return return
293 return $return;
294 }
295 }