change query

This commit is contained in:
aina-esokia
2018-10-26 18:01:31 +04:00
parent 60339b6de1
commit b407a17bdf
2 changed files with 65 additions and 24 deletions

View File

@@ -1264,7 +1264,7 @@ class V1Controller extends Controller
$selections = $this->findDataboxById($databoxId)
->getRecordRepository()
->findChildren($storyIds, $user,0, $story_max_items);
->findChildren($storyIds, $user,1, $story_max_items);
$children[$databoxId] = array_combine($storyIds, $selections);
}
@@ -1655,7 +1655,7 @@ class V1Controller extends Controller
$max_items = (int)$request->get('max_items')?:10;
$page = (int)$request->get('page')?:1;
$offset = $max_items * ($page - 1);
$offset = $max_items * ($page - 1) + 1;
}
$caption = $story->get_caption();

View File

@@ -120,7 +120,7 @@ class LegacyRecordRepository implements RecordRepository
return $this->mapRecordsFromResultSet($result);
}
public function findChildren(array $storyIds, $user = null, $offset = 0, $max_items = null)
public function findChildren(array $storyIds, $user = null, $offset = 1, $max_items = null)
{
if (!$storyIds) {
return [];
@@ -129,30 +129,71 @@ class LegacyRecordRepository implements RecordRepository
$connection = $this->databox->get_connection();
$selects = $this->getRecordSelects();
array_unshift($selects, 's.rid_parent as story_id', 's.ord');
$builder = $connection->createQueryBuilder();
$builder
->select($selects)
->from('regroup', 's')
->innerJoin('s', 'record', 'r', 'r.record_id = s.rid_child')
->where(
's.rid_parent IN (:storyIds)',
'r.parent_record_id = 0'
)
->setParameter('storyIds', $storyIds, Connection::PARAM_INT_ARRAY)
;
if (null !== $user) {
$this->addUserFilter($builder, $user);
}
if($max_items){
$builder->having('s.ord >= '. $offset .' and s.ord < '. ($offset + $max_items))
->addOrderBy('s.ord', 'ASC');
}
array_unshift($selects, 'sr.rid_parent as story_id');
$data = $connection->fetchAll($builder->getSQL(), $builder->getParameters(), $builder->getParameterTypes());
$subBuilder = $connection->createQueryBuilder();
$subBuilder
->select('s.*,
IF(@old_rid_parent != s.rid_parent, @cpt := 1, @cpt := @cpt+1) AS CPT')
->addSelect("IF(@old_rid_parent != s.rid_parent, IF(@old_rid_parent:=s.rid_parent,'NEW PARENT',0), '----------') AS Y")
->from('regroup', 's')
->where('s.rid_parent IN (:storyIds)')
->setParameter('storyIds', $storyIds, Connection::PARAM_INT_ARRAY)
->orderBy('s.rid_parent, s.ord')
;
$builder = $subBuilder->getConnection()->createQueryBuilder();
$builder->select($selects)
->from(sprintf('( %s )', $subBuilder->getSQL()), 'sr')
->innerJoin('sr', 'record', 'r', 'r.record_id = sr.rid_child')
->where('sr.CPT BETWEEN :offset AND :maxresult')
->andWhere('r.parent_record_id = 0')
->setParameter('offset', $offset)
->setParameter('maxresult', ($offset + $max_items -1))
->orderBy('story_id, sr.CPT')
;
if (null !== $user) {
$this->addUserFilter($builder, $user);
}
$connection->executeQuery('SET @cpt = 1');
$connection->executeQuery('SET @old_rid_parent = -1');
$data = $connection->fetchAll(
$builder->getSQL(),
array_merge($subBuilder->getParameters(), $builder->getParameters()),
array_merge($subBuilder->getParameterTypes(), $builder->getParameterTypes())
);
}else{
array_unshift($selects, 's.rid_parent as story_id');
$builder = $connection->createQueryBuilder();
$builder
->select($selects)
->from('regroup', 's')
->innerJoin('s', 'record', 'r', 'r.record_id = s.rid_child')
->where(
's.rid_parent IN (:storyIds)',
'r.parent_record_id = 0'
)
->setParameter('storyIds', $storyIds, Connection::PARAM_INT_ARRAY)
;
if (null !== $user) {
$this->addUserFilter($builder, $user);
}
$data = $connection->fetchAll($builder->getSQL(), $builder->getParameters(), $builder->getParameterTypes());
}
$records = $this->mapRecordsFromResultSet($data);