find by query sth

This commit is contained in:
yuri
2019-02-21 16:20:23 +02:00
parent 0721bd5c27
commit d3b0e9ed5d
3 changed files with 21 additions and 3 deletions

View File

@@ -33,6 +33,7 @@ use \Espo\Core\Exceptions\Error;
class EntityManager
{
const STH_COLLECTION = 'sthCollection';
protected $pdo;

View File

@@ -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;

View File

@@ -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();