From: Raphaël Gertz <git@rapsys.eu>
Date: Sun, 25 Feb 2018 05:04:38 +0000 (+0100)
Subject: Add repository classes
X-Git-Tag: 0.1~66
X-Git-Url: https://git.rapsys.eu/blogbundle/commitdiff_plain/ba6ed1d2d45b58011d275ef5960fdb012b9d0b94

Add repository classes
---

diff --git a/Repository/ArticleRepository.php b/Repository/ArticleRepository.php
new file mode 100644
index 0000000..9dd1556
--- /dev/null
+++ b/Repository/ArticleRepository.php
@@ -0,0 +1,101 @@
+<?php
+
+namespace Rapsys\BlogBundle\Repository;
+
+/**
+ * ArticleRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class ArticleRepository extends \Doctrine\ORM\EntityRepository {
+	/**
+	 * Find article
+	 *
+	 * @param string $_locale
+	 * @param string $_article
+	 */
+	public function findArticle($_locale, $_article) {
+#		//TODO: voir comment faire ça sans gros hack sql ?
+#		//Declare all field
+#		$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
+#		$rsm->addScalarResult('id', 'id');
+#		$rsm->addScalarResult('created', 'created');
+#		$rsm->addScalarResult('updated', 'updated');
+#		$rsm->addScalarResult('slug', 'slug');
+#		$rsm->addScalarResult('title', 'title');
+#		$rsm->addScalarResult('description', 'description');
+#		$rsm->addScalarResult('body', 'body');
+#		$rsm->addScalarResult('keywords', 'keywords');
+#		$rsm->addScalarResult('slugs', 'slugs');
+#
+#		//Fetch article
+#		$ret = $this->getEntityManager()
+#			#->createQuery('SELECT b.*, JSON(bl.iso6391, bat.slug) AS slugs FROM (SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, at.body, JSON(kt.slug, kt.title) AS keywords FROM RapsysBlogBundle:Article a JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = a.id AND at.slug = :_article) JOIN RapsysBlogBundle:Language AS l WITH (l.id = at.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.article_id = a.id) LEFT JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id) b LEFT JOIN RapsysBlogBundle:Language AS bl WITH (bl.iso6391 <> :_locale) LEFT JOIN RapsysBlogBundle:ArticleTranslation bat WITH (bat.language_id = bl.id AND bat.article_id = b.id) GROUP BY b.id')
+#			->createNativeQuery('SELECT b.*, CONCAT(\'{\', GROUP_CONCAT(CONCAT(\'"\', REPLACE(bl.iso6391, \'"\', \'\\\\"\'), \'": "\', REPLACE(bat.slug, \'"\', \'\\\\"\'), \'"\')), \'}\') AS slugs FROM (SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, at.body, CONCAT(\'{\', GROUP_CONCAT(CONCAT(\'"\', REPLACE(kt.slug, \'"\', \'\\\\"\'), \'": "\', REPLACE(kt.title, \'"\', \'\\\\"\'), \'"\')), \'}\') AS keywords FROM articles a JOIN articles_translations at ON (at.article_id = a.id AND at.slug = ?) JOIN languages AS l ON (l.id = at.language_id AND l.iso6391 = ?) LEFT JOIN articles_keywords ak ON (ak.article_id = a.id) LEFT JOIN keywords_translations kt ON (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id) b LEFT JOIN languages AS bl ON (bl.iso6391 <> ?) LEFT JOIN articles_translations bat ON (bat.language_id = bl.id AND bat.article_id = b.id) GROUP BY b.id', $rsm)
+#			->setParameter(0, $_article)
+#			->setParameter(1, $_locale)
+#			->setParameter(2, $_locale)
+#			->getSingleResult();
+#		//Decode json
+#		if (!empty($ret['keywords'])) {
+#			$ret['keywords'] = json_decode($ret['keywords'], true);
+#		}
+#		if (!empty($ret['slugs'])) {
+#			$ret['slugs'] = json_decode($ret['slugs'], true);
+#		}
+#		//Send result
+#		return $ret;
+#
+		//Fetch article
+		$ret = $this->getEntityManager()
+			#->createQuery('SELECT b.*, JSON(bl.iso6391, bat.slug) AS slugs FROM (SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, at.body, JSON(kt.slug, kt.title) AS keywords FROM RapsysBlogBundle:Article a JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = a.id AND at.slug = :_article) JOIN RapsysBlogBundle:Language AS l WITH (l.id = at.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.article_id = a.id) LEFT JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id) b LEFT JOIN RapsysBlogBundle:Language AS bl WITH (bl.iso6391 <> :_locale) LEFT JOIN RapsysBlogBundle:ArticleTranslation bat WITH (bat.language_id = bl.id AND bat.article_id = b.id) GROUP BY b.id')
+			->createQuery('SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, at.body, JSON(kt.slug, kt.title) AS keywords FROM RapsysBlogBundle:Article a JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = a.id AND at.slug = :_article) JOIN RapsysBlogBundle:Language AS l WITH (l.id = at.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.article_id = a.id) LEFT JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id')
+			->setParameter('_locale', $_locale)
+			->setParameter('_article', $_article)
+			->getSingleResult();
+
+		//Decode json
+		if (!empty($ret['keywords'])) {
+			$ret['keywords'] = json_decode($ret['keywords'], true);
+		}
+
+		//Fetch article's slugs in other locale
+		$slugs = $this->getEntityManager()
+			->createQuery('SELECT JSON(bl.iso6391, bat.slug) AS slugs FROM RapsysBlogBundle:Language bl LEFT JOIN RapsysBlogBundle:ArticleTranslation bat WITH (bat.article_id = :_article AND bat.language_id = bl.id) WHERE (bl.iso6391 <> :_locale) GROUP BY bat.article_id')
+			->setParameter('_locale', $_locale)
+			->setParameter('_article', $ret['id'])
+			->getSingleResult();
+
+		//Decode json
+		if (!empty($slugs['slugs'])) {
+			$ret['slugs'] = json_decode($slugs['slugs'], true);
+		}
+
+		//Send result
+		return $ret;
+	}
+
+	/**
+	 * Find articles
+	 *
+	 * @param string $_locale
+	 */
+	public function findArticles($_locale) {
+		//Fetch articles
+		$ret = $this->getEntityManager()
+                        ->createQuery('SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, JSON(kt.slug, kt.title) AS keywords FROM RapsysBlogBundle:Article a JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = a.id) JOIN RapsysBlogBundle:Language AS l WITH (l.id = at.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.article_id = a.id) LEFT JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id')
+                        ->setParameter('_locale', $_locale)
+			->execute();
+		//Decode json
+		if (!empty($ret)) {
+			foreach ($ret as $id => $article) {
+				if (!empty($article['keywords'])) {
+					$ret[$id]['keywords'] = json_decode($article['keywords'], true);
+				}
+			}
+		}
+		//Send result
+		return $ret;
+	}
+}
diff --git a/Repository/ArticleTranslationRepository.php b/Repository/ArticleTranslationRepository.php
new file mode 100644
index 0000000..f93e82e
--- /dev/null
+++ b/Repository/ArticleTranslationRepository.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Rapsys\BlogBundle\Repository;
+
+/**
+ * ArticleTranslationRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class ArticleTranslationRepository extends \Doctrine\ORM\EntityRepository
+{
+}
diff --git a/Repository/AuthorRepository.php b/Repository/AuthorRepository.php
new file mode 100644
index 0000000..1f298ad
--- /dev/null
+++ b/Repository/AuthorRepository.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Rapsys\BlogBundle\Repository;
+
+/**
+ * AuthorRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class AuthorRepository extends \Doctrine\ORM\EntityRepository
+{
+}
diff --git a/Repository/KeywordRepository.php b/Repository/KeywordRepository.php
new file mode 100644
index 0000000..08e0d22
--- /dev/null
+++ b/Repository/KeywordRepository.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace Rapsys\BlogBundle\Repository;
+
+/**
+ * KeywordRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class KeywordRepository extends \Doctrine\ORM\EntityRepository {
+	/**
+	 * Find keyword
+	 *
+	 * @param string $_locale
+	 * @param string $_keyword
+	 */
+	public function findKeyword($_locale, $_keyword) {
+		//Fetch keyword
+		$ret = $this->getEntityManager()
+			->createQuery('SELECT k.id, k.created, k.updated, kt.slug, kt.title, kt.description, JSON(at.slug, at.title) AS articles, \'\' AS slugs FROM RapsysBlogBundle:Keyword k JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = k.id AND kt.slug = :_keyword) JOIN RapsysBlogBundle:Language AS l WITH (l.id = kt.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.keyword_id = k.id) LEFT JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = ak.article_id AND at.language_id = kt.language_id) GROUP BY k.id')
+			->setParameter('_locale', $_locale)
+			->setParameter('_keyword', $_keyword)
+			->getSingleResult();
+
+		//Decode json
+		if (!empty($ret['articles'])) {
+			$ret['articles'] = json_decode($ret['articles'], true);
+		}
+
+		//Fetch keyword's slugs in other locale
+		$slugs = $this->getEntityManager()
+			->createQuery('SELECT JSON(bl.iso6391, bkt.slug) AS slugs FROM RapsysBlogBundle:Language bl LEFT JOIN RapsysBlogBundle:KeywordTranslation bkt WITH (bkt.keyword_id = :_keyword AND bkt.language_id = bl.id) WHERE (bl.iso6391 <> :_locale) GROUP BY bkt.keyword_id')
+			->setParameter('_locale', $_locale)
+			->setParameter('_keyword', $ret['id'])
+			->getSingleResult();
+
+		//Decode json
+		if (!empty($slugs['slugs'])) {
+			$ret['slugs'] = json_decode($slugs['slugs'], true);
+		}
+
+		//Send result
+		return $ret;
+	}
+
+	/**
+	 * Find keywords
+	 *
+	 * @param string $_locale
+	 */
+	public function findKeywords($_locale) {
+		//Fetch keywords
+		$ret = $this->getEntityManager()
+			#, JSON(kt.keyword_id, kt.title) AS keywords
+                        ->createQuery('SELECT k.id, k.created, k.updated, kt.slug, kt.title, JSON(at.slug, at.title) AS articles FROM RapsysBlogBundle:Keyword k JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = k.id) JOIN RapsysBlogBundle:Language AS l WITH (l.id = kt.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.keyword_id = k.id) LEFT JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = ak.article_id AND at.language_id = kt.language_id) GROUP BY k.id')
+                        ->setParameter('_locale', $_locale)
+			->execute();
+		//Decode json
+		if (!empty($ret)) {
+			foreach ($ret as $id => $keyword) {
+				if (!empty($keyword['articles'])) {
+					$ret[$id]['articles'] = json_decode($keyword['articles'], true);
+				}
+			}
+		}
+		//Send result
+		return $ret;
+	}
+}
diff --git a/Repository/KeywordTranslationRepository.php b/Repository/KeywordTranslationRepository.php
new file mode 100644
index 0000000..be5a1b3
--- /dev/null
+++ b/Repository/KeywordTranslationRepository.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Rapsys\BlogBundle\Repository;
+
+/**
+ * KeywordTranslationRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class KeywordTranslationRepository extends \Doctrine\ORM\EntityRepository
+{
+}
diff --git a/Repository/LanguageRepository.php b/Repository/LanguageRepository.php
new file mode 100644
index 0000000..5d14d1d
--- /dev/null
+++ b/Repository/LanguageRepository.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Rapsys\BlogBundle\Repository;
+
+/**
+ * LanguageRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class LanguageRepository extends \Doctrine\ORM\EntityRepository
+{
+}
diff --git a/Repository/SiteRepository.php b/Repository/SiteRepository.php
new file mode 100644
index 0000000..924d417
--- /dev/null
+++ b/Repository/SiteRepository.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Rapsys\BlogBundle\Repository;
+
+/**
+ * SiteRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class SiteRepository extends \Doctrine\ORM\EntityRepository
+{
+}
diff --git a/Repository/SiteTranslationRepository.php b/Repository/SiteTranslationRepository.php
new file mode 100644
index 0000000..44015b9
--- /dev/null
+++ b/Repository/SiteTranslationRepository.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Rapsys\BlogBundle\Repository;
+
+/**
+ * SiteTranslationRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class SiteTranslationRepository extends \Doctrine\ORM\EntityRepository
+{
+}