]> Raphaël G. Git Repositories - airbundle/blobdiff - Repository/DanceRepository.php
Add missing headers arround titles
[airbundle] / Repository / DanceRepository.php
index 8296ccd14e9e3cdfabb7389dadb69b2ab8891e80..24ab2b4452c44c779ef9452c883c5aa460ce1513 100644 (file)
-<?php
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys AirBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
 
 namespace Rapsys\AirBundle\Repository;
 
-use Symfony\Component\Translation\TranslatorInterface;
 use Doctrine\ORM\Query\ResultSetMapping;
 
 /**
  * DanceRepository
  */
-class DanceRepository extends \Doctrine\ORM\EntityRepository {
+class DanceRepository extends EntityRepository {
+       /**
+        * Find dance names as array
+        *
+        * @return array The dance names
+        */
+       public function findNamesAsArray() {
+               //Set the request
+               $req = <<<SQL
+SELECT
+       d.name,
+       GROUP_CONCAT(d.id ORDER BY d.id SEPARATOR "\\n") AS ids,
+       GROUP_CONCAT(d.type ORDER BY d.id SEPARATOR "\\n") AS types,
+       MAX(d.updated) AS modified
+FROM RapsysAirBundle:Dance AS d
+GROUP BY d.name
+ORDER BY d.name
+SQL;
+
+               //Replace bundle entity name by table name
+               $req = str_replace($this->tableKeys, $this->tableValues, $req);
+
+               //Get result set mapping instance
+               //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
+               $rsm = new ResultSetMapping();
+
+               //Declare all fields
+               //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
+               //addScalarResult($sqlColName, $resColName, $type = 'string');
+               $rsm->addScalarResult('name', 'name', 'string')
+                       ->addScalarResult('ids', 'ids', 'string')
+                       ->addScalarResult('types', 'types', 'string')
+                       ->addScalarResult('modified', 'modified', 'datetime')
+                       ->addIndexByScalar('name');
+
+               //Get result
+               $result = $this->_em
+                       ->createNativeQuery($req, $rsm)
+                       ->getArrayResult();
+
+               //Set return
+               $return = [];
+
+               //Iterate on each name
+               foreach($result as $name) {
+                       //Set name slug
+                       $slug = $this->slugger->slug($tname = $this->translator->trans($name['name']));
+
+                       //Set types
+                       $types = [];
+
+                       //Explode ids
+                       $name['ids'] = explode("\n", $name['ids']);
+
+                       //Explode types
+                       $name['types'] = explode("\n", $name['types']);
+
+                       //Iterate on each type
+                       foreach($name['ids'] as $k => $id) {
+                               //Add to types
+                               $types[$this->slugger->short($name['types'][$k])] = [
+                                       'id' => $id,
+                                       'type' => $type = $this->translator->trans($name['types'][$k]),
+                                       'slug' => $stype = $this->slugger->slug($type),
+                                       'link' => $this->router->generate('rapsys_air_dance_view', ['id' => $id, 'name' => $slug, 'type' => $stype])
+                               ];
+                       }
+
+                       //Add to return
+                       $return[$sname = $this->slugger->short($name['name'])] = [
+                               'name' => $tname,
+                               'slug' => $slug,
+                               'link' => $this->router->generate('rapsys_air_dance_name', ['name' => $sname, 'dance' => $slug]),
+                               'types' => $types,
+                               'modified' => $name['modified']
+                       ];
+               }
+
+               //Return return
+               return $return;
+       }
+
        /**
         * Find dances by user id
         *
@@ -16,28 +105,14 @@ class DanceRepository extends \Doctrine\ORM\EntityRepository {
         * @return array The user dances
         */
        public function findByUserId($userId) {
-               //Get entity manager
-               $em = $this->getEntityManager();
-
-               //Get quote strategy
-               $qs = $em->getConfiguration()->getQuoteStrategy();
-               $dp = $em->getConnection()->getDatabasePlatform();
-
-               //Get quoted table names
-               //XXX: this allow to make this code table name independent
-               $tables = [
-                       'RapsysAirBundle:UserDance' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('dances'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
-                       'RapsysAirBundle:Dance' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Dance'), $dp)
-               ];
-
                //Set the request
-               $req = 'SELECT d.id, d.title
+               $req = 'SELECT d.id, d.name, d.type
 FROM RapsysAirBundle:UserDance AS ud
 JOIN RapsysAirBundle:Dance AS d ON (d.id = ud.dance_id)
 WHERE ud.user_id = :uid';
 
                //Replace bundle entity name by table name
-               $req = str_replace(array_keys($tables), array_values($tables), $req);
+               $req = str_replace($this->tableKeys, $this->tableValues, $req);
 
                //Get result set mapping instance
                //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
@@ -46,10 +121,11 @@ WHERE ud.user_id = :uid';
                //Declare result set for our request
                $rsm->addEntityResult('RapsysAirBundle:Dance', 'd');
                $rsm->addFieldResult('d', 'id', 'id');
-               $rsm->addFieldResult('d', 'title', 'title');
+               $rsm->addFieldResult('d', 'name', 'name');
+               $rsm->addFieldResult('d', 'type', 'type');
 
                //Send result
-               return $em
+               return $this->_em
                        ->createNativeQuery($req, $rsm)
                        ->setParameter('uid', $userId)
                        ->getResult();