mirror of
https://github.com/Oreolek/ifhub.club.git
synced 2024-07-05 16:04:24 +03:00
ORM - add order and paging filter
This commit is contained in:
parent
360fd642b1
commit
13d62b14f5
|
@ -76,6 +76,49 @@ class MapperORM extends Mapper {
|
||||||
public function GetItemsByFilter($aFilter,$sEntityFull) {
|
public function GetItemsByFilter($aFilter,$sEntityFull) {
|
||||||
$sTableName = self::GetTableName($sEntityFull);
|
$sTableName = self::GetTableName($sEntityFull);
|
||||||
|
|
||||||
|
// Сортировка
|
||||||
|
$sOrder='';
|
||||||
|
if (isset($aFilter['#order'])) {
|
||||||
|
foreach ($aFilter['#order'] as $key=>$value) {
|
||||||
|
if (is_numeric($key)) {
|
||||||
|
$key=$value;
|
||||||
|
$value='asc';
|
||||||
|
} elseif (!in_array($value,array('asc','desc'))) {
|
||||||
|
$value='asc';
|
||||||
|
}
|
||||||
|
$sOrder.=" {$key} {$value},";
|
||||||
|
}
|
||||||
|
$sOrder=trim($sOrder,',');
|
||||||
|
if ($sOrder!='') {
|
||||||
|
$sOrder="ORDER BY {$sOrder}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Постраничность
|
||||||
|
if (isset($aFilter['#page']) and is_array($aFilter['#page']) and count($aFilter['#page'])==2) { // array(2,15) - 2 - page, 15 - count
|
||||||
|
$aFilter['#limit']=array(($aFilter['#page'][0]-1)*$aFilter['#page'][1],$aFilter['#page'][1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Лимит
|
||||||
|
$sLimit='';
|
||||||
|
if (isset($aFilter['#limit'])) { // допустимы варианты: limit=10 , limit=array(10) , limit=array(10,15)
|
||||||
|
$aLimit=$aFilter['#limit'];
|
||||||
|
if (is_numeric($aLimit)) {
|
||||||
|
$iBegin=0;
|
||||||
|
$iEnd=$aLimit;
|
||||||
|
} elseif (is_array($aLimit)) {
|
||||||
|
if (count($aLimit)>1) {
|
||||||
|
$iBegin=$aLimit[0];
|
||||||
|
$iEnd=$aLimit[1];
|
||||||
|
} else {
|
||||||
|
$iBegin=0;
|
||||||
|
$iEnd=$aLimit[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$sLimit="LIMIT {$iBegin}, {$iEnd}";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$aFilterFields=array();
|
$aFilterFields=array();
|
||||||
foreach ($aFilter as $k=>$v) {
|
foreach ($aFilter as $k=>$v) {
|
||||||
if (substr($k,0,1)=='#') {
|
if (substr($k,0,1)=='#') {
|
||||||
|
@ -90,10 +133,8 @@ class MapperORM extends Mapper {
|
||||||
if (count($aFilterFields)) {
|
if (count($aFilterFields)) {
|
||||||
$sFilterFields=' and '.implode(' = ? and ',array_keys($aFilterFields)).' = ? ';
|
$sFilterFields=' and '.implode(' = ? and ',array_keys($aFilterFields)).' = ? ';
|
||||||
}
|
}
|
||||||
|
$sql = "SELECT * FROM ".$sTableName." WHERE 1=1 {$sFilterFields} {$sOrder} {$sLimit} ";
|
||||||
$sql = "SELECT * FROM ".$sTableName." WHERE 1=1 {$sFilterFields} ";
|
|
||||||
$aQueryParams=array_merge(array($sql),array_values($aFilterFields));
|
$aQueryParams=array_merge(array($sql),array_values($aFilterFields));
|
||||||
|
|
||||||
$aItems=array();
|
$aItems=array();
|
||||||
if($aRows=call_user_func_array(array($this->oDb,'select'),$aQueryParams)) {
|
if($aRows=call_user_func_array(array($this->oDb,'select'),$aQueryParams)) {
|
||||||
foreach($aRows as $aRow) {
|
foreach($aRows as $aRow) {
|
||||||
|
@ -105,6 +146,32 @@ class MapperORM extends Mapper {
|
||||||
return $aItems;
|
return $aItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function GetCountItemsByFilter($aFilter,$sEntityFull) {
|
||||||
|
$sTableName = self::GetTableName($sEntityFull);
|
||||||
|
|
||||||
|
$aFilterFields=array();
|
||||||
|
foreach ($aFilter as $k=>$v) {
|
||||||
|
if (substr($k,0,1)=='#') {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$aFilterFields[$k]=$v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$sFilterFields='';
|
||||||
|
if (count($aFilterFields)) {
|
||||||
|
$sFilterFields=' and '.implode(' = ? and ',array_keys($aFilterFields)).' = ? ';
|
||||||
|
}
|
||||||
|
$sql = "SELECT count(*) as c FROM ".$sTableName." WHERE 1=1 {$sFilterFields} ";
|
||||||
|
$aQueryParams=array_merge(array($sql),array_values($aFilterFields));
|
||||||
|
if($aRow=call_user_func_array(array($this->oDb,'selectRow'),$aQueryParams)) {
|
||||||
|
return $aRow['c'];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function GetItemsByArray($aFilter,$sEntityFull) {
|
public function GetItemsByArray($aFilter,$sEntityFull) {
|
||||||
$sTableName = self::GetTableName($sEntityFull);
|
$sTableName = self::GetTableName($sEntityFull);
|
||||||
|
|
||||||
|
|
|
@ -142,8 +142,11 @@ abstract class ModuleORM extends Module {
|
||||||
$sRelModuleName=Engine::GetModuleName($oRelEntityEmpty);
|
$sRelModuleName=Engine::GetModuleName($oRelEntityEmpty);
|
||||||
$sRelEntityName=Engine::GetEntityName($oRelEntityEmpty);
|
$sRelEntityName=Engine::GetEntityName($oRelEntityEmpty);
|
||||||
$sRelPluginPrefix=Engine::GetPluginPrefix($oRelEntityEmpty);
|
$sRelPluginPrefix=Engine::GetPluginPrefix($oRelEntityEmpty);
|
||||||
// ItemsByArrayId - id пока идет костылем, т.к. у стандартных сущностей нет метода _GetPrimaryKey()
|
$sPrimaryKey="Id";
|
||||||
$aRelData=Engine::GetInstance()->_CallModule("{$sRelPluginPrefix}{$sRelModuleName}_get{$sRelEntityName}ItemsByArrayId",array($aEntityKeys[$sRelKey]));
|
if ($sPrimaryKey=$oRelEntityEmpty->_GetPrimaryKey()) {
|
||||||
|
$sPrimaryKey=ucfirst($sPrimaryKey);
|
||||||
|
}
|
||||||
|
$aRelData=Engine::GetInstance()->_CallModule("{$sRelPluginPrefix}{$sRelModuleName}_get{$sRelEntityName}ItemsByArray{$sPrimaryKey}",array($aEntityKeys[$sRelKey]));
|
||||||
/**
|
/**
|
||||||
* Собираем набор
|
* Собираем набор
|
||||||
*/
|
*/
|
||||||
|
@ -156,9 +159,27 @@ abstract class ModuleORM extends Module {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Если запрашиваем постраничный список, то возвращаем сам список и общее количество записей
|
||||||
|
*/
|
||||||
|
if (isset($aFilter['#page'])) {
|
||||||
|
return array('collection'=>$aEntities,'count'=>$this->GetCountItemsByFilter($aFilter,$sEntityFull));
|
||||||
|
}
|
||||||
return $aEntities;
|
return $aEntities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function GetCountItemsByFilter($aFilter=array(),$sEntityFull=null) {
|
||||||
|
if (is_null($sEntityFull)) {
|
||||||
|
$sEntityFull=Engine::GetPluginPrefix($this).'Module'.Engine::GetModuleName($this).'_Entity'.Engine::GetModuleName(get_class($this));
|
||||||
|
} elseif (!substr_count($sEntityFull,'_')) {
|
||||||
|
$sEntityFull=Engine::GetPluginPrefix($this).'Module'.Engine::GetModuleName($this).'_Entity'.$sEntityFull;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iCount=$this->oMapperORM->GetCountItemsByFilter($aFilter,$sEntityFull);
|
||||||
|
|
||||||
|
return $iCount;
|
||||||
|
}
|
||||||
|
|
||||||
public function GetItemsByArray($aFilter,$sEntityFull=null) {
|
public function GetItemsByArray($aFilter,$sEntityFull=null) {
|
||||||
if (is_null($sEntityFull)) {
|
if (is_null($sEntityFull)) {
|
||||||
$sEntityFull=Engine::GetPluginPrefix($this).'Module'.Engine::GetModuleName($this).'_Entity'.Engine::GetModuleName(get_class($this));
|
$sEntityFull=Engine::GetPluginPrefix($this).'Module'.Engine::GetModuleName($this).'_Entity'.Engine::GetModuleName(get_class($this));
|
||||||
|
@ -240,7 +261,12 @@ abstract class ModuleORM extends Module {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
if (preg_match("@^get_([a-z]+)((_items)|())_by_([_a-z]+)$@i",$sNameUnderscore,$aMatch)) {
|
if (preg_match("@^get_([a-z]+)((_items)|())_by_([_a-z]+)$@i",$sNameUnderscore,$aMatch)) {
|
||||||
$aFilter=array_combine(explode('_and_',$aMatch[5]),$aArgs);
|
$aSearchParams=explode('_and_',$aMatch[5]);
|
||||||
|
$aSplit=array_chunk($aArgs,count($aSearchParams));
|
||||||
|
$aFilter=array_combine($aSearchParams,$aSplit[0]);
|
||||||
|
if (isset($aSplit[1][0])) {
|
||||||
|
$aFilter=array_merge($aFilter,$aSplit[1][0]);
|
||||||
|
}
|
||||||
if ($aMatch[2]=='_items') {
|
if ($aMatch[2]=='_items') {
|
||||||
return $this->GetItemsByFilter($aFilter,$sEntityName);
|
return $this->GetItemsByFilter($aFilter,$sEntityName);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue