1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-01 14:05:07 +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:
Alexander Zinchuk 2010-11-13 05:43:25 +00:00
parent c32e37889f
commit b703fad0fa
3 changed files with 117 additions and 44 deletions

View file

@ -27,6 +27,8 @@ abstract class EntityORM extends Entity {
const RELATION_TYPE_MANY_TO_MANY='many_to_many';
const RELATION_TYPE_TREE='tree';
protected $aFields=array();
protected $aRelations=array();
protected $aRelationsData=array();
@ -34,17 +36,17 @@ abstract class EntityORM extends Entity {
protected $bIsNew=true;
public function __construct($aParam = false) {
public function __construct($aParam=false) {
parent::__construct($aParam);
}
public function _GetPrimaryKey() {
if(!$this->_getDataOne($this->sPrimaryKey)) {
$sModulePrefix=null;
if (preg_match('/Entity([^_]+)/',get_class($this),$sModulePrefix)) {
$sModulePrefix=func_underscore($sModulePrefix[1]).'_';
if($this->_getDataOne($sModulePrefix.$this->sPrimaryKey)) {
$this->sPrimaryKey=$sModulePrefix.$this->sPrimaryKey;
if($this->_getFields()) {
if(array_key_exists('#primary_key',$this->aFields)) {
$this->sPrimaryKey = $this->aFields['#primary_key'];
} else {
$this->sPrimaryKey = $this->_getField($this->sPrimaryKey);
}
}
}
@ -77,6 +79,10 @@ abstract class EntityORM extends Entity {
public function Reload() {
return $this->_Method(__FUNCTION__);
}
public function ShowColumns() {
return $this->_Method(__FUNCTION__ .'From');
}
public function getChildren() {
@ -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() {
return $this->aRelations;
}
@ -184,11 +219,9 @@ abstract class EntityORM extends Entity {
if (isset($this->_aData[$sKey])) {
return $this->_aData[$sKey];
} else {
if (preg_match('/Entity([^_]+)/',get_class($this),$sModulePrefix)) {
$sModulePrefix=func_underscore($sModulePrefix[1]).'_';
if (isset($this->_aData[$sModulePrefix.$sKey])) {
return $this->_aData[$sModulePrefix.$sKey];
}
$sField=$this->_getField($sKey);
if($sField!=$sKey && isset($this->_aData[$sField])) {
return $this->_aData[$sField];
}
}
/**
@ -238,12 +271,11 @@ abstract class EntityORM extends Entity {
break;
case self::RELATION_TYPE_MANY_TO_MANY :
$sCmd="{$sRelPluginPrefix}Module{$sRelModuleName}_get{$sRelEntityName}ItemsByJoinTable";
$sByKey = strpos($this->_GetPrimaryKey(), $sModulePrefix) === 0 ? $this->_GetPrimaryKey() : $sModulePrefix.$this->_GetPrimaryKey();
$aCmdArgs[0] = array(
'join_table' => $sRelationJoinTable,
'join_table' => $sRelationJoinTable,
'relation_key' => $sRelationKey,
'by_key' => $sByKey,
'by_value' => $iPrimaryKeyValue,
'by_key' => $this->_GetPrimaryKey(),
'by_value' => $iPrimaryKeyValue,
);
break;
default:
@ -260,7 +292,7 @@ abstract class EntityORM extends Entity {
if (array_key_exists($sKey,$this->aRelations)) {
$this->aRelationsData[$sKey]=$aArgs[0];
} else {
$this->_aData[$sKey]=$aArgs[0];
$this->_aData[$this->_getField($sKey)]=$aArgs[0];
}
} elseif ($sType=='reload') {
if (array_key_exists($sKey,$this->aRelationsData)) {

View file

@ -46,6 +46,7 @@ class MapperORM extends Mapper {
}
public function GetByFilter($aFilter,$sEntityFull) {
$oEntitySample=Engine::GetEntity($sEntityFull);
$sTableName = self::GetTableName($sEntityFull);
$aFilterFields=array();
@ -53,7 +54,7 @@ class MapperORM extends Mapper {
if (substr($k,0,1)=='#') {
} else {
$aFilterFields[$k]=$v;
$aFilterFields[$oEntitySample->_getField($k)]=$v;
}
}
@ -74,6 +75,7 @@ class MapperORM extends Mapper {
}
public function GetItemsByFilter($aFilter,$sEntityFull) {
$oEntitySample=Engine::GetEntity($sEntityFull);
$sTableName = self::GetTableName($sEntityFull);
// Сортировка
@ -124,7 +126,7 @@ class MapperORM extends Mapper {
if (substr($k,0,1)=='#') {
} else {
$aFilterFields[$k]=$v;
$aFilterFields[$oEntitySample->_getField($k)]=$v;
}
}
@ -148,6 +150,7 @@ class MapperORM extends Mapper {
public function GetCountItemsByFilter($aFilter,$sEntityFull) {
$oEntitySample=Engine::GetEntity($sEntityFull);
$sTableName = self::GetTableName($sEntityFull);
$aFilterFields=array();
@ -155,7 +158,7 @@ class MapperORM extends Mapper {
if (substr($k,0,1)=='#') {
} else {
$aFilterFields[$k]=$v;
$aFilterFields[$oEntitySample->_getField($k)]=$v;
}
}
@ -173,9 +176,13 @@ class MapperORM extends Mapper {
public function GetItemsByArray($aFilter,$sEntityFull) {
$oEntitySample=Engine::GetEntity($sEntityFull);
$sTableName = self::GetTableName($sEntityFull);
$aFilterFields=$aFilter;
$aFilterFields=array();
foreach($aFilter as $k=>$v) {
$aFilterFields[$oEntitySample->_getField($k)]=$v;
}
$sFilterFields='';
if (count($aFilterFields)) {
@ -197,9 +204,13 @@ class MapperORM extends Mapper {
}
public function GetCountItemsByArray($aFilter,$sEntityFull) {
$oEntitySample=Engine::GetEntity($sEntityFull);
$sTableName = self::GetTableName($sEntityFull);
$aFilterFields=$aFilter;
$aFilterFields=array();
foreach($aFilter as $k=>$v) {
$aFilterFields[$oEntitySample->_getField($k)]=$v;
}
$sFilterFields='';
if (count($aFilterFields)) {
@ -216,6 +227,7 @@ class MapperORM extends Mapper {
return 0;
}
public function GetItemsByJoinTable($aData,$sEntityFull) {
if(empty($aData)) {
return null;
@ -223,7 +235,7 @@ class MapperORM extends Mapper {
$sTableName = self::GetTableName($sEntityFull);
$sql = "SELECT a.*, b.* FROM ?# a LEFT JOIN ".$sTableName." b USING(?#) WHERE a.?#=?";
$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) {
$oEntity=Engine::GetEntity($sEntityFull,$aRow);
$oEntity->_SetIsNew(false);
@ -233,6 +245,21 @@ class MapperORM extends Mapper {
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) {
/**
* Варианты таблиц:

View file

@ -32,7 +32,6 @@ abstract class ModuleORM extends Module {
}
protected function _AddEntity($oEntity) {
$res=$this->oMapperORM->AddEntity($oEntity);
if ($res===0) {
@ -57,16 +56,7 @@ abstract class ModuleORM extends Module {
}
return false;
}
protected function _DeleteEntity($oEntity) {
$res=$this->oMapperORM->DeleteEntity($oEntity);
if ($res) {
return $oEntity;
}
return false;
}
protected function _SaveEntity($oEntity) {
if ($oEntity->_isNew()) {
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) {
if($sPrimaryKey=$oEntity->_getPrimaryKey()) {
@ -88,7 +85,13 @@ abstract class ModuleORM extends Module {
}
return false;
}
protected function _ShowColumnsFrom($oEntity) {
$res=$this->oMapperORM->ShowColumnsFrom($oEntity);
return $res;
}
protected function _GetChildrenOfEntity($oEntity) {
if(in_array(EntityORM::RELATION_TYPE_TREE,$oEntity->_getRelations())) {
@ -357,27 +360,31 @@ abstract class ModuleORM extends Module {
if (preg_match("@^delete([\w]+)$@i",$sName,$aMatch)) {
return $this->_DeleteEntity($aArgs[0]);
}
}
if (preg_match("@^reload([\w]+)$@i",$sName,$aMatch)) {
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)) {
return $this->_GetChildrenOfEntity($aArgs[0]);
}
}
if (preg_match("@^getparentof([\w]+)$@i",$sName,$aMatch)) {
return $this->_GetParentOfEntity($aArgs[0]);
}
}
if (preg_match("@^getdescendantsof([\w]+)$@i",$sName,$aMatch)) {
return $this->_GetDescendantsOfEntity($aArgs[0]);
}
}
if (preg_match("@^getancestorsof([\w]+)$@i",$sName,$aMatch)) {
return $this->_GetAncestorsOfEntity($aArgs[0]);
}
}
if (preg_match("@^loadtreeof([\w]+)$@i",$sName,$aMatch)) {
$sEntityFull = array_key_exists(1,$aMatch) ? $aMatch[1] : null;
@ -385,19 +392,26 @@ abstract class ModuleORM extends Module {
}
$sNameUnderscore=func_underscore($sName);
$iEntityPosEnd=strlen($sNameUnderscore)-1;
if(substr_count($sNameUnderscore,'_items')) {
$iEntityPosEnd=0;
if(strpos($sNameUnderscore,'_items')>4) {
$iEntityPosEnd=strpos($sNameUnderscore,'_items');
} else if(substr_count($sNameUnderscore,'_by')) {
} else if(strpos($sNameUnderscore,'_by')>4) {
$iEntityPosEnd=strpos($sNameUnderscore,'_by');
} else if(substr_count($sNameUnderscore,'_all')) {
} else if(strpos($sNameUnderscore,'_all')>4) {
$iEntityPosEnd=strpos($sNameUnderscore,'_all');
}
$sEntityName=substr($sNameUnderscore,4,$iEntityPosEnd-4);
if($iEntityPosEnd) {
$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
*/
$sNameUnderscore=substr_replace($sNameUnderscore,str_replace('_','',$sEntityName),4,$iEntityPosEnd-4);
$sEntityName=func_camelize($sEntityName);
/**
* getUserItemsByArrayId() get_user_items_by_array_id