diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 8899e9fe0d..2eeab6fa4c 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -33,6 +33,7 @@ use \Espo\Core\Exceptions\Error; class EntityManager { + const STH_COLLECTION = 'sthCollection'; protected $pdo; diff --git a/application/Espo/ORM/Repositories/RDB.php b/application/Espo/ORM/Repositories/RDB.php index 2ee0c947b0..ed50a246e5 100644 --- a/application/Espo/ORM/Repositories/RDB.php +++ b/application/Espo/ORM/Repositories/RDB.php @@ -227,11 +227,17 @@ class RDB extends \Espo\ORM\Repository return null; } - public function findByQuery($sql) + public function findByQuery(string $sql, ?string $collectionType = null) { $dataArr = $this->getMapper()->selectByQuery($this->seed, $sql); - $collection = new EntityCollection($dataArr, $this->entityType, $this->entityFactory); + if (!$collectionType) { + $collection = new EntityCollection($dataArr, $this->entityType, $this->entityFactory); + } else if ($collectionType === \Espo\ORM\EntityManager::STH_COLLECTION) { + $collection = $this->getEntityManager()->createSthCollection($this->entityType); + $collection->setQuery($sql); + } + $this->reset(); return $collection; diff --git a/application/Espo/ORM/SthCollection.php b/application/Espo/ORM/SthCollection.php index 8a3446c44f..5c6e11a088 100644 --- a/application/Espo/ORM/SthCollection.php +++ b/application/Espo/ORM/SthCollection.php @@ -39,6 +39,8 @@ class SthCollection implements \IteratorAggregate private $sth = null; + private $sql = null; + public function __construct(string $entityType, EntityManager $entityManager = null, array $selectParams = []) { $this->selectParams = $selectParams; @@ -51,9 +53,18 @@ class SthCollection implements \IteratorAggregate $this->selectParams = $selectParams; } + public function setQuery(?string $sql) + { + $this->sql = $sql; + } + public function executeQuery() { - $sql = $this->entityManager->getQuery()->createSelectQuery($this->entityType, $this->selectParams); + if ($this->sql) { + $sql = $this->sql; + } else { + $sql = $this->entityManager->getQuery()->createSelectQuery($this->entityType, $this->selectParams); + } $sth = $this->entityManager->getPdo()->prepare($sql); $sth->execute();