From: Raphaël Gertz Date: Sun, 3 Nov 2024 04:43:14 +0000 (+0100) Subject: Import repository base class X-Git-Tag: 0.0.7~1 X-Git-Url: https://git.rapsys.eu/treebundle/commitdiff_plain/2cec3e449469a1981d46d7700ed5c9c50c242595?ds=sidebyside;hp=c74ab618abe6dad2df2e2e2139cec77188da6208 Import repository base class Import album repository class --- diff --git a/Repository.php b/Repository.php new file mode 100644 index 0000000..b60959f --- /dev/null +++ b/Repository.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Rapsys\TreeBundle; + +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\Mapping\ClassMetadata; + +use Psr\Container\ContainerInterface; + +use Rapsys\PackBundle\Util\SluggerUtil; + +use Symfony\Component\Routing\RouterInterface; +use Symfony\Contracts\Translation\TranslatorInterface; + +/** + * Repository + * + * {@inheritdoc} + */ +class Repository extends EntityRepository { + /** + * The table keys array + * + * @var array + */ + protected array $keys; + + /** + * The table names array + * + * @var array + */ + protected array $names; + + /** + * Initializes a new LocationRepository instance + * + * @param EntityManagerInterface $manager The EntityManagerInterface instance + * @param ClassMetadata $class The ClassMetadata instance + * @param ContainerInterface $container The container instance + * @param RouterInterface $router The router instance + * @param SluggerUtil $slugger The SluggerUtil instance + * @param TranslatorInterface $translator The TranslatorInterface instance + * @param string $locale The current locale + * @param array $languages The languages list + */ + public function __construct(protected EntityManagerInterface $manager, protected ClassMetadata $class, protected ContainerInterface $container, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected string $locale, protected array $languages) { + //Call parent constructor + parent::__construct($this->manager, $this->class); + + //Get quote strategy + $qs = $this->manager->getConfiguration()->getQuoteStrategy(); + $dp = $this->manager->getConnection()->getDatabasePlatform(); + + //Set quoted table names + //XXX: this allow to make this code table name independent + //XXX: remember to place longer prefix before shorter to avoid strange replacings + //XXX: entity short syntax removed in doctrine/persistence 3.x: https://github.com/doctrine/orm/issues/8818 + $tables = [ + 'Rapsys\TreeBundle\Entity\UserGroup' => $qs->getJoinTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\User')->getAssociationMapping('groups'), $manager->getClassMetadata('Rapsys\TreeBundle\Entity\User'), $dp), + 'Rapsys\TreeBundle\Entity\Album' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Album'), $dp), + 'Rapsys\TreeBundle\Entity\Asset' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Asset'), $dp), + 'Rapsys\TreeBundle\Entity\Civility' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Civility'), $dp), + 'Rapsys\TreeBundle\Entity\Group' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\Group'), $dp), + 'Rapsys\TreeBundle\Entity\User' => $qs->getTableName($manager->getClassMetadata('Rapsys\TreeBundle\Entity\User'), $dp), + //Set locale + //XXX: or $manager->getConnection()->quote($this->locale) ??? + ':locale' => $dp->quoteStringLiteral($this->locale), + //Set limit + //XXX: Set limit used to workaround mariadb subselect optimization + ':limit' => PHP_INT_MAX, + //Set cleanup + "\t" => '', + "\r" => ' ', + "\n" => ' ' + ]; + + //Set quoted table keys + $this->keys = array_keys($tables); + + //Set quoted table names + $this->names = array_values($tables); + } + + /** + * Get replaced query + * + * @param string $req The request to replace + * @return string The replaced request + */ + protected function replace(string $req): string { + //Replace bundle entity name by table name + return str_replace($this->keys, $this->names, $req); + } +} diff --git a/Repository/AlbumRepository.php b/Repository/AlbumRepository.php new file mode 100644 index 0000000..c1901d9 --- /dev/null +++ b/Repository/AlbumRepository.php @@ -0,0 +1,170 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Rapsys\TreeBundle\Repository; + +use Doctrine\ORM\Query\ResultSetMapping; + +use Rapsys\TreeBundle\Repository; + +/** + * Album repository + */ +class AlbumRepository extends Repository { + /** + * Find album count as int + * + * @param ?integer $id The user id + * @return integer The albums count + */ + public function findCountAsInt(?int $id): int { + //Set user sql + $userSql = <<replace($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('count', 'count', 'integer'); + + //Get result + return $this->_em + ->createNativeQuery($req, $rsm) + ->setParameter('id', $id) + ->getSingleScalarResult(); + } + + /** + * Find albums as array + * + * @param ?integer $id The user id + * @param integer $page The page + * @param integer $count The count + * @return array The albums array + */ + public function findAllAsArray(?int $id, int $page, int $count): array { + //Set user sql + $userSql = <<replace($req); + + //Get result set mapping instance + $rsm = new ResultSetMapping(); + + //Declare all fields + //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php + //addScalarResult($sqlColName, $resColName, $type = 'string'); + $rsm->addScalarResult('id', 'id', 'integer') + ->addScalarResult('path', 'path', 'string') + ->addScalarResult('slug', 'slug', 'string') + ->addScalarResult('s_ids', 's_ids', 'string') + ->addScalarResult('s_paths', 's_paths', 'string') + ->addScalarResult('created', 'created', 'datetime') + ->addScalarResult('updated', 'updated', 'datetime') + ->addScalarResult('modified', 'modified', 'datetime') + ->addIndexByScalar('id'); + + //Get result + $result = $this->_em + ->createNativeQuery($req, $rsm) + ->setParameter('offset', $page * $count) + ->setParameter('count', $count) + ->setParameter('id', $id) + ->getArrayResult(); + + //Set return + $return = []; + + //Iterate on each city + foreach($result as $data) { + //Add to return + $return[$data['id']] = [ + 'id' => $id = $data['id'], + 'slug' => $slug = $data['slug'], + 'path' => $data['path'], + 'created' => $data['created'], + 'updated' => $data['updated'], + 'modified' => $data['modified'], + 'link' => $this->router->generate('rapsystree_album', ['id' => $id, 'slug' => $slug, 'path' => '/']), + 'assets' => [] + ]; + + //Explode asset ids + $data['s_ids'] = explode("\n", $data['s_ids']); + + //Explode asset paths + $data['s_paths'] = explode("\n", $data['s_paths']); + + foreach($data['s_ids'] as $s => $id) { + $return[$data['id']]['assets'][$id] = [ + 'id' => $id, + 'path' => $path = $data['s_paths'][$s], + 'link' => $this->router->generate('rapsystree_asset', ['id' => $id, 'path' => $path]), + ]; + } + } + + //Return return + return $return; + } +}