]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Repository/DanceRepository.php
Use getObject event args member function instead of deprecated getEntity
[airbundle] / Repository / DanceRepository.php
index 24ab2b4452c44c779ef9452c883c5aa460ce1513..98a6ee9d8e6c2549c90bd31e4274c0f0c9228bed 100644 (file)
 
 namespace Rapsys\AirBundle\Repository;
 
+use Doctrine\ORM\AbstractQuery;
 use Doctrine\ORM\Query\ResultSetMapping;
 
+use Rapsys\AirBundle\Repository;
+
 /**
  * DanceRepository
  */
-class DanceRepository extends EntityRepository {
+class DanceRepository extends Repository {
+       /**
+        * Find dances indexed by id
+        *
+        * @return array The dances
+        */
+       public function findAllIndexed(): array {
+               //Set the request
+               $req = <<<SQL
+SELECT
+       d.id,
+       d.name,
+       d.type
+FROM Rapsys\AirBundle\Entity\Dance AS d
+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->addEntityResult('Rapsys\AirBundle\Entity\Dance', 'd')
+                       ->addFieldResult('d', 'id', 'id')
+                       ->addFieldResult('d', 'name', 'name')
+                       ->addFieldResult('d', 'type', 'type')
+                       ->addIndexByColumn('d', 'id');
+
+               //Return return
+               return $this->_em
+                       ->createNativeQuery($req, $rsm)
+                       ->getResult();
+       }
+
+       /**
+        * Find dance choices as array
+        *
+        * @return array The dance choices
+        */
+       public function findChoicesAsArray(): array {
+               //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
+FROM Rapsys\AirBundle\Entity\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')
+                       ->addIndexByScalar('name');
+
+               //Get result
+               $result = $this->_em
+                       ->createNativeQuery($req, $rsm)
+                       ->getArrayResult();
+
+               //Set return
+               $return = [];
+
+               //Iterate on each name
+               foreach($result as $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->translator->trans($name['types'][$k]).' ('.$id.')'] = intval($id);
+                       }
+
+                       //Add to return
+                       $return[$this->translator->trans($name['name'])] = $types;
+               }
+
+               //Return return
+               return $return;
+       }
+
+       /**
+        * Find dances ids by nametype
+        *
+        * @param array $nametype The nametype filter
+        * @return array The dance ids
+        */
+       public function findIdByNameTypeAsArray(array $nametype): array {
+               //Set the request
+               $req = <<<SQL
+SELECT
+       d.id
+FROM Rapsys\AirBundle\Entity\Dance AS d
+WHERE CONCAT_WS(' ', d.name, d.type) IN (:nametype)
+ORDER BY d.name, d.type
+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
+               //XXX: we don't use a result set as we want to translate group and civility
+               $rsm->addScalarResult('id', 'id', 'integer');
+
+               //Return result
+               return $this->_em
+                       ->createNativeQuery($req, $rsm)
+                       ->setParameter('nametype', $nametype)
+                       //XXX: instead of array_column on the result
+                       ->getResult(AbstractQuery::HYDRATE_SCALAR_COLUMN);
+       }
+
        /**
         * Find dance names as array
         *
         * @return array The dance names
         */
-       public function findNamesAsArray() {
+       public function findNamesAsArray(): array {
                //Set the request
                $req = <<<SQL
 SELECT
@@ -30,7 +171,7 @@ SELECT
        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
+FROM Rapsys\AirBundle\Entity\Dance AS d
 GROUP BY d.name
 ORDER BY d.name
 SQL;
@@ -104,11 +245,11 @@ SQL;
         * @param $id The user id
         * @return array The user dances
         */
-       public function findByUserId($userId) {
+       public function findByUserId($userId): array {
                //Set the request
                $req = 'SELECT d.id, d.name, d.type
-FROM RapsysAirBundle:UserDance AS ud
-JOIN RapsysAirBundle:Dance AS d ON (d.id = ud.dance_id)
+FROM Rapsys\AirBundle\Entity\UserDance AS ud
+JOIN Rapsys\AirBundle\Entity\Dance AS d ON (d.id = ud.dance_id)
 WHERE ud.user_id = :uid';
 
                //Replace bundle entity name by table name
@@ -119,7 +260,7 @@ WHERE ud.user_id = :uid';
                $rsm = new ResultSetMapping();
 
                //Declare result set for our request
-               $rsm->addEntityResult('RapsysAirBundle:Dance', 'd');
+               $rsm->addEntityResult('Rapsys\AirBundle\Entity\Dance', 'd');
                $rsm->addFieldResult('d', 'id', 'id');
                $rsm->addFieldResult('d', 'name', 'name');
                $rsm->addFieldResult('d', 'type', 'type');