mirror of
https://github.com/Oreolek/ifhub.club.git
synced 2024-07-06 00:14:25 +03:00
ORM/ActiveRecoed usability upgrade.
Now we can use: Sample_GetItemsByParam1AndParam2('foo','bar') instead of Sample_GetSampleItemsBySampleParam1AndSampleParam2('foo','bar') $oSample->setTitle()/getTitle() instead of $oSample->setSampleTitle()/getSampleTitle() Primary key definition improved.
This commit is contained in:
parent
c32e37889f
commit
b703fad0fa
|
@ -27,6 +27,8 @@ abstract class EntityORM extends Entity {
|
||||||
const RELATION_TYPE_MANY_TO_MANY='many_to_many';
|
const RELATION_TYPE_MANY_TO_MANY='many_to_many';
|
||||||
const RELATION_TYPE_TREE='tree';
|
const RELATION_TYPE_TREE='tree';
|
||||||
|
|
||||||
|
protected $aFields=array();
|
||||||
|
|
||||||
protected $aRelations=array();
|
protected $aRelations=array();
|
||||||
protected $aRelationsData=array();
|
protected $aRelationsData=array();
|
||||||
|
|
||||||
|
@ -34,17 +36,17 @@ abstract class EntityORM extends Entity {
|
||||||
protected $bIsNew=true;
|
protected $bIsNew=true;
|
||||||
|
|
||||||
|
|
||||||
public function __construct($aParam = false) {
|
public function __construct($aParam=false) {
|
||||||
parent::__construct($aParam);
|
parent::__construct($aParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function _GetPrimaryKey() {
|
public function _GetPrimaryKey() {
|
||||||
if(!$this->_getDataOne($this->sPrimaryKey)) {
|
if(!$this->_getDataOne($this->sPrimaryKey)) {
|
||||||
$sModulePrefix=null;
|
if($this->_getFields()) {
|
||||||
if (preg_match('/Entity([^_]+)/',get_class($this),$sModulePrefix)) {
|
if(array_key_exists('#primary_key',$this->aFields)) {
|
||||||
$sModulePrefix=func_underscore($sModulePrefix[1]).'_';
|
$this->sPrimaryKey = $this->aFields['#primary_key'];
|
||||||
if($this->_getDataOne($sModulePrefix.$this->sPrimaryKey)) {
|
} else {
|
||||||
$this->sPrimaryKey=$sModulePrefix.$this->sPrimaryKey;
|
$this->sPrimaryKey = $this->_getField($this->sPrimaryKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,6 +81,10 @@ abstract class EntityORM extends Entity {
|
||||||
return $this->_Method(__FUNCTION__);
|
return $this->_Method(__FUNCTION__);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function ShowColumns() {
|
||||||
|
return $this->_Method(__FUNCTION__ .'From');
|
||||||
|
}
|
||||||
|
|
||||||
public function getChildren() {
|
public function getChildren() {
|
||||||
if(in_array(self::RELATION_TYPE_TREE,$this->aRelations)) {
|
if(in_array(self::RELATION_TYPE_TREE,$this->aRelations)) {
|
||||||
return $this->_Method(__FUNCTION__ .'Of');
|
return $this->_Method(__FUNCTION__ .'Of');
|
||||||
|
@ -164,6 +170,35 @@ abstract class EntityORM extends Entity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function _getFields() {
|
||||||
|
if(empty($this->aFields)) {
|
||||||
|
$this->aFields=$this->ShowColumns();
|
||||||
|
}
|
||||||
|
return $this->aFields;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function _getField($sField) {
|
||||||
|
if($aFields=$this->_getFields()) {
|
||||||
|
if(in_array($sField,$aFields)) {
|
||||||
|
return $sField;
|
||||||
|
}
|
||||||
|
$sFieldU = func_camelize($sField);
|
||||||
|
$sEntityField = func_underscore(Engine::GetEntityName($this).$sFieldU);
|
||||||
|
if(in_array($sEntityField,$aFields)) {
|
||||||
|
return $sEntityField;
|
||||||
|
}
|
||||||
|
$sModuleEntityField = func_underscore(Engine::GetModuleName($this).Engine::GetEntityName($this).$sFieldU);
|
||||||
|
if(in_array($sModuleEntityField,$aFields)) {
|
||||||
|
return $sModuleEntityField;
|
||||||
|
}
|
||||||
|
$sModuleField = func_underscore(Engine::GetModuleName($this).$sFieldU);
|
||||||
|
if(in_array($sModuleField,$aFields)) {
|
||||||
|
return $sModuleField;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $sField;
|
||||||
|
}
|
||||||
|
|
||||||
public function _getRelations() {
|
public function _getRelations() {
|
||||||
return $this->aRelations;
|
return $this->aRelations;
|
||||||
}
|
}
|
||||||
|
@ -184,11 +219,9 @@ abstract class EntityORM extends Entity {
|
||||||
if (isset($this->_aData[$sKey])) {
|
if (isset($this->_aData[$sKey])) {
|
||||||
return $this->_aData[$sKey];
|
return $this->_aData[$sKey];
|
||||||
} else {
|
} else {
|
||||||
if (preg_match('/Entity([^_]+)/',get_class($this),$sModulePrefix)) {
|
$sField=$this->_getField($sKey);
|
||||||
$sModulePrefix=func_underscore($sModulePrefix[1]).'_';
|
if($sField!=$sKey && isset($this->_aData[$sField])) {
|
||||||
if (isset($this->_aData[$sModulePrefix.$sKey])) {
|
return $this->_aData[$sField];
|
||||||
return $this->_aData[$sModulePrefix.$sKey];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -238,11 +271,10 @@ abstract class EntityORM extends Entity {
|
||||||
break;
|
break;
|
||||||
case self::RELATION_TYPE_MANY_TO_MANY :
|
case self::RELATION_TYPE_MANY_TO_MANY :
|
||||||
$sCmd="{$sRelPluginPrefix}Module{$sRelModuleName}_get{$sRelEntityName}ItemsByJoinTable";
|
$sCmd="{$sRelPluginPrefix}Module{$sRelModuleName}_get{$sRelEntityName}ItemsByJoinTable";
|
||||||
$sByKey = strpos($this->_GetPrimaryKey(), $sModulePrefix) === 0 ? $this->_GetPrimaryKey() : $sModulePrefix.$this->_GetPrimaryKey();
|
|
||||||
$aCmdArgs[0] = array(
|
$aCmdArgs[0] = array(
|
||||||
'join_table' => $sRelationJoinTable,
|
'join_table' => $sRelationJoinTable,
|
||||||
'relation_key' => $sRelationKey,
|
'relation_key' => $sRelationKey,
|
||||||
'by_key' => $sByKey,
|
'by_key' => $this->_GetPrimaryKey(),
|
||||||
'by_value' => $iPrimaryKeyValue,
|
'by_value' => $iPrimaryKeyValue,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
@ -260,7 +292,7 @@ abstract class EntityORM extends Entity {
|
||||||
if (array_key_exists($sKey,$this->aRelations)) {
|
if (array_key_exists($sKey,$this->aRelations)) {
|
||||||
$this->aRelationsData[$sKey]=$aArgs[0];
|
$this->aRelationsData[$sKey]=$aArgs[0];
|
||||||
} else {
|
} else {
|
||||||
$this->_aData[$sKey]=$aArgs[0];
|
$this->_aData[$this->_getField($sKey)]=$aArgs[0];
|
||||||
}
|
}
|
||||||
} elseif ($sType=='reload') {
|
} elseif ($sType=='reload') {
|
||||||
if (array_key_exists($sKey,$this->aRelationsData)) {
|
if (array_key_exists($sKey,$this->aRelationsData)) {
|
||||||
|
|
|
@ -46,6 +46,7 @@ class MapperORM extends Mapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function GetByFilter($aFilter,$sEntityFull) {
|
public function GetByFilter($aFilter,$sEntityFull) {
|
||||||
|
$oEntitySample=Engine::GetEntity($sEntityFull);
|
||||||
$sTableName = self::GetTableName($sEntityFull);
|
$sTableName = self::GetTableName($sEntityFull);
|
||||||
|
|
||||||
$aFilterFields=array();
|
$aFilterFields=array();
|
||||||
|
@ -53,7 +54,7 @@ class MapperORM extends Mapper {
|
||||||
if (substr($k,0,1)=='#') {
|
if (substr($k,0,1)=='#') {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$aFilterFields[$k]=$v;
|
$aFilterFields[$oEntitySample->_getField($k)]=$v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,6 +75,7 @@ class MapperORM extends Mapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function GetItemsByFilter($aFilter,$sEntityFull) {
|
public function GetItemsByFilter($aFilter,$sEntityFull) {
|
||||||
|
$oEntitySample=Engine::GetEntity($sEntityFull);
|
||||||
$sTableName = self::GetTableName($sEntityFull);
|
$sTableName = self::GetTableName($sEntityFull);
|
||||||
|
|
||||||
// Сортировка
|
// Сортировка
|
||||||
|
@ -124,7 +126,7 @@ class MapperORM extends Mapper {
|
||||||
if (substr($k,0,1)=='#') {
|
if (substr($k,0,1)=='#') {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$aFilterFields[$k]=$v;
|
$aFilterFields[$oEntitySample->_getField($k)]=$v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,6 +150,7 @@ class MapperORM extends Mapper {
|
||||||
|
|
||||||
|
|
||||||
public function GetCountItemsByFilter($aFilter,$sEntityFull) {
|
public function GetCountItemsByFilter($aFilter,$sEntityFull) {
|
||||||
|
$oEntitySample=Engine::GetEntity($sEntityFull);
|
||||||
$sTableName = self::GetTableName($sEntityFull);
|
$sTableName = self::GetTableName($sEntityFull);
|
||||||
|
|
||||||
$aFilterFields=array();
|
$aFilterFields=array();
|
||||||
|
@ -155,7 +158,7 @@ class MapperORM extends Mapper {
|
||||||
if (substr($k,0,1)=='#') {
|
if (substr($k,0,1)=='#') {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$aFilterFields[$k]=$v;
|
$aFilterFields[$oEntitySample->_getField($k)]=$v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,9 +176,13 @@ class MapperORM extends Mapper {
|
||||||
|
|
||||||
|
|
||||||
public function GetItemsByArray($aFilter,$sEntityFull) {
|
public function GetItemsByArray($aFilter,$sEntityFull) {
|
||||||
|
$oEntitySample=Engine::GetEntity($sEntityFull);
|
||||||
$sTableName = self::GetTableName($sEntityFull);
|
$sTableName = self::GetTableName($sEntityFull);
|
||||||
|
|
||||||
$aFilterFields=$aFilter;
|
$aFilterFields=array();
|
||||||
|
foreach($aFilter as $k=>$v) {
|
||||||
|
$aFilterFields[$oEntitySample->_getField($k)]=$v;
|
||||||
|
}
|
||||||
|
|
||||||
$sFilterFields='';
|
$sFilterFields='';
|
||||||
if (count($aFilterFields)) {
|
if (count($aFilterFields)) {
|
||||||
|
@ -197,9 +204,13 @@ class MapperORM extends Mapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function GetCountItemsByArray($aFilter,$sEntityFull) {
|
public function GetCountItemsByArray($aFilter,$sEntityFull) {
|
||||||
|
$oEntitySample=Engine::GetEntity($sEntityFull);
|
||||||
$sTableName = self::GetTableName($sEntityFull);
|
$sTableName = self::GetTableName($sEntityFull);
|
||||||
|
|
||||||
$aFilterFields=$aFilter;
|
$aFilterFields=array();
|
||||||
|
foreach($aFilter as $k=>$v) {
|
||||||
|
$aFilterFields[$oEntitySample->_getField($k)]=$v;
|
||||||
|
}
|
||||||
|
|
||||||
$sFilterFields='';
|
$sFilterFields='';
|
||||||
if (count($aFilterFields)) {
|
if (count($aFilterFields)) {
|
||||||
|
@ -216,6 +227,7 @@ class MapperORM extends Mapper {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function GetItemsByJoinTable($aData,$sEntityFull) {
|
public function GetItemsByJoinTable($aData,$sEntityFull) {
|
||||||
if(empty($aData)) {
|
if(empty($aData)) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -223,7 +235,7 @@ class MapperORM extends Mapper {
|
||||||
$sTableName = self::GetTableName($sEntityFull);
|
$sTableName = self::GetTableName($sEntityFull);
|
||||||
$sql = "SELECT a.*, b.* FROM ?# a LEFT JOIN ".$sTableName." b USING(?#) WHERE a.?#=?";
|
$sql = "SELECT a.*, b.* FROM ?# a LEFT JOIN ".$sTableName." b USING(?#) WHERE a.?#=?";
|
||||||
$aItems = array();
|
$aItems = array();
|
||||||
if($aRows=$this->oDb->select($sql, $aData['join_table'], $aData['relation_key'], $aData['by_key'], $aData['by_value'])) {
|
if($aRows=$this->oDb->select($sql, $aData['join_table'],$aData['relation_key'],$aData['by_key'],$aData['by_value'])) {
|
||||||
foreach($aRows as $aRow) {
|
foreach($aRows as $aRow) {
|
||||||
$oEntity=Engine::GetEntity($sEntityFull,$aRow);
|
$oEntity=Engine::GetEntity($sEntityFull,$aRow);
|
||||||
$oEntity->_SetIsNew(false);
|
$oEntity->_SetIsNew(false);
|
||||||
|
@ -233,6 +245,21 @@ class MapperORM extends Mapper {
|
||||||
return $aItems;
|
return $aItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function ShowColumnsFrom($oEntity) {
|
||||||
|
$sTableName = self::GetTableName($oEntity);
|
||||||
|
$sql = "SHOW COLUMNS FROM ".$sTableName;
|
||||||
|
$aItems = array();
|
||||||
|
if($aRows=$this->oDb->select($sql)) {
|
||||||
|
foreach($aRows as $aRow) {
|
||||||
|
$aItems[] = $aRow['Field'];
|
||||||
|
if($aRow['Key']=='PRI') {
|
||||||
|
$aItems['#primary_key'] = $aRow['Field'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $aItems;
|
||||||
|
}
|
||||||
|
|
||||||
public static function GetTableName($oEntity) {
|
public static function GetTableName($oEntity) {
|
||||||
/**
|
/**
|
||||||
* Варианты таблиц:
|
* Варианты таблиц:
|
||||||
|
|
|
@ -32,7 +32,6 @@ abstract class ModuleORM extends Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected function _AddEntity($oEntity) {
|
protected function _AddEntity($oEntity) {
|
||||||
$res=$this->oMapperORM->AddEntity($oEntity);
|
$res=$this->oMapperORM->AddEntity($oEntity);
|
||||||
if ($res===0) {
|
if ($res===0) {
|
||||||
|
@ -58,15 +57,6 @@ abstract class ModuleORM extends Module {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function _DeleteEntity($oEntity) {
|
|
||||||
$res=$this->oMapperORM->DeleteEntity($oEntity);
|
|
||||||
if ($res) {
|
|
||||||
return $oEntity;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected function _SaveEntity($oEntity) {
|
protected function _SaveEntity($oEntity) {
|
||||||
if ($oEntity->_isNew()) {
|
if ($oEntity->_isNew()) {
|
||||||
return $this->_AddEntity($oEntity);
|
return $this->_AddEntity($oEntity);
|
||||||
|
@ -75,6 +65,13 @@ abstract class ModuleORM extends Module {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function _DeleteEntity($oEntity) {
|
||||||
|
$res=$this->oMapperORM->DeleteEntity($oEntity);
|
||||||
|
if ($res) {
|
||||||
|
return $oEntity;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
protected function _ReloadEntity($oEntity) {
|
protected function _ReloadEntity($oEntity) {
|
||||||
if($sPrimaryKey=$oEntity->_getPrimaryKey()) {
|
if($sPrimaryKey=$oEntity->_getPrimaryKey()) {
|
||||||
|
@ -90,6 +87,12 @@ abstract class ModuleORM extends Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected function _ShowColumnsFrom($oEntity) {
|
||||||
|
$res=$this->oMapperORM->ShowColumnsFrom($oEntity);
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function _GetChildrenOfEntity($oEntity) {
|
protected function _GetChildrenOfEntity($oEntity) {
|
||||||
if(in_array(EntityORM::RELATION_TYPE_TREE,$oEntity->_getRelations())) {
|
if(in_array(EntityORM::RELATION_TYPE_TREE,$oEntity->_getRelations())) {
|
||||||
$aRelationsData=$oEntity->_getRelationsData();
|
$aRelationsData=$oEntity->_getRelationsData();
|
||||||
|
@ -363,6 +366,10 @@ abstract class ModuleORM extends Module {
|
||||||
return $this->_ReloadEntity($aArgs[0]);
|
return $this->_ReloadEntity($aArgs[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (preg_match("@^showcolumnsfrom([\w]+)$@i",$sName,$aMatch)) {
|
||||||
|
return $this->_ShowColumnsFrom($aArgs[0]);
|
||||||
|
}
|
||||||
|
|
||||||
if (preg_match("@^getchildrenof([\w]+)$@i",$sName,$aMatch)) {
|
if (preg_match("@^getchildrenof([\w]+)$@i",$sName,$aMatch)) {
|
||||||
return $this->_GetChildrenOfEntity($aArgs[0]);
|
return $this->_GetChildrenOfEntity($aArgs[0]);
|
||||||
}
|
}
|
||||||
|
@ -385,19 +392,26 @@ abstract class ModuleORM extends Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
$sNameUnderscore=func_underscore($sName);
|
$sNameUnderscore=func_underscore($sName);
|
||||||
$iEntityPosEnd=strlen($sNameUnderscore)-1;
|
$iEntityPosEnd=0;
|
||||||
if(substr_count($sNameUnderscore,'_items')) {
|
if(strpos($sNameUnderscore,'_items')>4) {
|
||||||
$iEntityPosEnd=strpos($sNameUnderscore,'_items');
|
$iEntityPosEnd=strpos($sNameUnderscore,'_items');
|
||||||
} else if(substr_count($sNameUnderscore,'_by')) {
|
} else if(strpos($sNameUnderscore,'_by')>4) {
|
||||||
$iEntityPosEnd=strpos($sNameUnderscore,'_by');
|
$iEntityPosEnd=strpos($sNameUnderscore,'_by');
|
||||||
} else if(substr_count($sNameUnderscore,'_all')) {
|
} else if(strpos($sNameUnderscore,'_all')>4) {
|
||||||
$iEntityPosEnd=strpos($sNameUnderscore,'_all');
|
$iEntityPosEnd=strpos($sNameUnderscore,'_all');
|
||||||
}
|
}
|
||||||
|
if($iEntityPosEnd) {
|
||||||
$sEntityName=substr($sNameUnderscore,4,$iEntityPosEnd-4);
|
$sEntityName=substr($sNameUnderscore,4,$iEntityPosEnd-4);
|
||||||
|
} else {
|
||||||
|
$sEntityName=func_underscore(Engine::GetModuleName($this)).'_';
|
||||||
|
$sNameUnderscore=substr_replace($sNameUnderscore,$sEntityName,4,0);
|
||||||
|
$iEntityPosEnd=strlen($sEntityName)-1+4;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* getUserRoleJoinByUserIdAndRoleId() get_user-role-join_by_user_id_and_role_id
|
* getUserRoleJoinByUserIdAndRoleId() get_user-role-join_by_user_id_and_role_id
|
||||||
*/
|
*/
|
||||||
$sNameUnderscore=substr_replace($sNameUnderscore,str_replace('_','',$sEntityName),4,$iEntityPosEnd-4);
|
$sNameUnderscore=substr_replace($sNameUnderscore,str_replace('_','',$sEntityName),4,$iEntityPosEnd-4);
|
||||||
|
|
||||||
$sEntityName=func_camelize($sEntityName);
|
$sEntityName=func_camelize($sEntityName);
|
||||||
/**
|
/**
|
||||||
* getUserItemsByArrayId() get_user_items_by_array_id
|
* getUserItemsByArrayId() get_user_items_by_array_id
|
||||||
|
|
Loading…
Reference in a new issue