mirror of
https://github.com/Oreolek/ifhub.club.git
synced 2024-07-01 05:55:02 +03:00
Обновление many-to-many
This commit is contained in:
parent
4233f8a709
commit
9d321e1ad2
|
@ -19,33 +19,33 @@
|
|||
* Абстрактный класс сущности ORM
|
||||
*
|
||||
*/
|
||||
abstract class EntityORM extends Entity {
|
||||
|
||||
abstract class EntityORM extends Entity {
|
||||
|
||||
const RELATION_TYPE_BELONGS_TO='belongs_to';
|
||||
const RELATION_TYPE_HAS_MANY='has_many';
|
||||
const RELATION_TYPE_HAS_ONE='has_one';
|
||||
const RELATION_TYPE_MANY_TO_MANY='many_to_many';
|
||||
const RELATION_TYPE_TREE='tree';
|
||||
|
||||
protected $_aOriginalData=array();
|
||||
|
||||
|
||||
protected $_aOriginalData=array();
|
||||
|
||||
protected $aFields=array();
|
||||
|
||||
|
||||
protected $aRelations=array();
|
||||
protected $aRelationsData=array();
|
||||
|
||||
// Объекты связей many_to_many
|
||||
protected $_aManyToManyRelations = array();
|
||||
|
||||
|
||||
protected $sPrimaryKey='id';
|
||||
protected $bIsNew=true;
|
||||
|
||||
|
||||
|
||||
|
||||
public function __construct($aParam=false) {
|
||||
parent::__construct($aParam);
|
||||
$this->aRelations=$this->_getRelations();
|
||||
}
|
||||
|
||||
|
||||
public function _getPrimaryKey() {
|
||||
if(!$this->_getDataOne($this->sPrimaryKey)) {
|
||||
if($this->_getFields()) {
|
||||
|
@ -62,15 +62,15 @@ abstract class EntityORM extends Entity {
|
|||
public function _getPrimaryKeyValue() {
|
||||
return $this->_getDataOne($this->_getPrimaryKey());
|
||||
}
|
||||
|
||||
|
||||
public function _isNew() {
|
||||
return $this->bIsNew;
|
||||
}
|
||||
|
||||
|
||||
public function _SetIsNew($bIsNew) {
|
||||
$this->bIsNew=$bIsNew;
|
||||
}
|
||||
|
||||
|
||||
public function Add() {
|
||||
if ($this->beforeSave())
|
||||
if ($res=$this->_Method(__FUNCTION__)) {
|
||||
|
@ -79,7 +79,7 @@ abstract class EntityORM extends Entity {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function Update() {
|
||||
if ($this->beforeSave())
|
||||
if ($res=$this->_Method(__FUNCTION__)) {
|
||||
|
@ -88,7 +88,7 @@ abstract class EntityORM extends Entity {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function Save() {
|
||||
if ($this->beforeSave())
|
||||
if ($res=$this->_Method(__FUNCTION__)) {
|
||||
|
@ -97,7 +97,7 @@ abstract class EntityORM extends Entity {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function Delete() {
|
||||
if ($this->beforeDelete())
|
||||
if ($res=$this->_Method(__FUNCTION__)) {
|
||||
|
@ -105,33 +105,33 @@ abstract class EntityORM extends Entity {
|
|||
return $res;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function Reload() {
|
||||
return $this->_Method(__FUNCTION__);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function ShowColumns() {
|
||||
return $this->_Method(__FUNCTION__ .'From');
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected function beforeSave() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected function afterSave() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected function beforeDelete() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected function afterDelete() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getChildren() {
|
||||
if(in_array(self::RELATION_TYPE_TREE,$this->aRelations)) {
|
||||
|
@ -139,7 +139,7 @@ abstract class EntityORM extends Entity {
|
|||
}
|
||||
return $this->__call(__FUNCTION__);
|
||||
}
|
||||
|
||||
|
||||
public function getDescendants() {
|
||||
if(in_array(self::RELATION_TYPE_TREE,$this->aRelations)) {
|
||||
return $this->_Method(__FUNCTION__ .'Of');
|
||||
|
@ -160,7 +160,7 @@ abstract class EntityORM extends Entity {
|
|||
}
|
||||
return $this->__call(__FUNCTION__);
|
||||
}
|
||||
|
||||
|
||||
public function setChildren($aChildren=array()) {
|
||||
if(in_array(self::RELATION_TYPE_TREE,$this->aRelations)) {
|
||||
$this->aRelationsData['children'] = $aChildren;
|
||||
|
@ -168,8 +168,8 @@ abstract class EntityORM extends Entity {
|
|||
$aArgs = func_get_args();
|
||||
return $this->__call(__FUNCTION__,$aArgs);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function setDescendants($aDescendants=array()) {
|
||||
if(in_array(self::RELATION_TYPE_TREE,$this->aRelations)) {
|
||||
$this->aRelationsData['descendants'] = $aDescendants;
|
||||
|
@ -187,7 +187,7 @@ abstract class EntityORM extends Entity {
|
|||
return $this->__call(__FUNCTION__,$aArgs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function setAncestors($oParent=null) {
|
||||
if(in_array(self::RELATION_TYPE_TREE,$this->aRelations)) {
|
||||
$this->aRelationsData['ancestors'] = $oParent;
|
||||
|
@ -196,23 +196,23 @@ abstract class EntityORM extends Entity {
|
|||
return $this->__call(__FUNCTION__,$aArgs);
|
||||
}
|
||||
}
|
||||
|
||||
protected function _Method($sName) {
|
||||
|
||||
protected function _Method($sName) {
|
||||
$sModuleName=Engine::GetModuleName($this);
|
||||
$sEntityName=Engine::GetEntityName($this);
|
||||
$sPluginPrefix=Engine::GetPluginPrefix($this);
|
||||
$sPluginPrefix=Engine::GetPluginPrefix($this);
|
||||
/**
|
||||
* If Module not exists, try to find its root Delegater
|
||||
*/
|
||||
$aClassInfo = Engine::GetClassInfo($sPluginPrefix.'Module_'.$sModuleName,Engine::CI_MODULE);
|
||||
if(empty($aClassInfo[Engine::CI_MODULE]) && $sRootDelegater=$this->Plugin_GetRootDelegater('entity',get_class($this))) {
|
||||
$sModuleName=Engine::GetModuleName($sRootDelegater);
|
||||
$sPluginPrefix=Engine::GetPluginPrefix($sRootDelegater);
|
||||
$sPluginPrefix=Engine::GetPluginPrefix($sRootDelegater);
|
||||
}
|
||||
return Engine::GetInstance()->_CallModule("{$sPluginPrefix}{$sModuleName}_{$sName}{$sEntityName}",array($this));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function _setData($aData) {
|
||||
if(is_array($aData)) {
|
||||
foreach ($aData as $sKey => $val) {
|
||||
|
@ -225,18 +225,18 @@ abstract class EntityORM extends Entity {
|
|||
$this->_aOriginalData = $this->_aData;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function _getOriginalData() {
|
||||
return $this->_aOriginalData;
|
||||
}
|
||||
|
||||
|
||||
public function _getFields() {
|
||||
if(empty($this->aFields)) {
|
||||
$this->aFields=$this->ShowColumns();
|
||||
}
|
||||
return $this->aFields;
|
||||
}
|
||||
|
||||
|
||||
public function _getField($sField,$iPersistence=3) {
|
||||
if($aFields=$this->_getFields()) {
|
||||
if(in_array($sField,$aFields)) {
|
||||
|
@ -267,7 +267,7 @@ abstract class EntityORM extends Entity {
|
|||
}
|
||||
return $sField;
|
||||
}
|
||||
|
||||
|
||||
public function _getRelations() {
|
||||
$sParent=get_parent_class($this);
|
||||
if(substr_count($sParent,'_Inherits_') || substr_count($sParent,'_Inherit_')) {
|
||||
|
@ -279,7 +279,7 @@ abstract class EntityORM extends Entity {
|
|||
$aParentRelations=$oEntityParent->_getRelations();
|
||||
}
|
||||
return array_merge($aParentRelations,$this->aRelations);
|
||||
}
|
||||
}
|
||||
|
||||
public function _getRelationsData() {
|
||||
return $this->aRelationsData;
|
||||
|
@ -287,11 +287,11 @@ abstract class EntityORM extends Entity {
|
|||
|
||||
public function _setRelationsData($aData) {
|
||||
$this->aRelationsData=$aData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function __call($sName,$aArgs) {
|
||||
$sType=substr($sName,0,strpos(func_underscore($sName),'_'));
|
||||
if (!strpos($sName,'_') and in_array($sType,array('get','set','reload'))) {
|
||||
if (!strpos($sName,'_') and in_array($sType,array('get','set','reload'))) {
|
||||
$sKey=func_underscore(str_replace($sType,'',$sName));
|
||||
if ($sType=='get') {
|
||||
if (isset($this->_aData[$sKey])) {
|
||||
|
@ -315,15 +315,15 @@ abstract class EntityORM extends Entity {
|
|||
$sRelationJoinTable=$this->aRelations[$sKey][3];
|
||||
$sRelationJoinTableKey=isset($this->aRelations[$sKey][4]) ? $this->aRelations[$sKey][4] : $this->_getPrimaryKey();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Если связь уже загруженна, то возвращаем сразу результат
|
||||
*/
|
||||
if (array_key_exists($sKey,$this->aRelationsData)) {
|
||||
return $this->aRelationsData[$sKey];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
$sRelModuleName=Engine::GetModuleName($sEntityRel);
|
||||
$sRelEntityName=Engine::GetEntityName($sEntityRel);
|
||||
$sRelPluginPrefix=Engine::GetPluginPrefix($sEntityRel);
|
||||
|
@ -331,7 +331,7 @@ abstract class EntityORM extends Entity {
|
|||
if($oRelEntity=Engine::GetEntity($sEntityRel) and method_exists($oRelEntity,'_getPrimaryKey')) { // для совместимости с сущностями Entity
|
||||
$sRelPrimaryKey=$oRelEntity->_getPrimaryKey();
|
||||
}
|
||||
|
||||
|
||||
$iPrimaryKeyValue=$this->_getDataOne($this->_getPrimaryKey());
|
||||
$sCmd='';
|
||||
$mCmdArgs=array();
|
||||
|
@ -378,7 +378,7 @@ abstract class EntityORM extends Entity {
|
|||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
} elseif ($sType=='set' and array_key_exists(0,$aArgs)) {
|
||||
if (array_key_exists($sKey,$this->aRelations)) {
|
||||
|
@ -415,5 +415,12 @@ abstract class EntityORM extends Entity {
|
|||
return $this->$sName;
|
||||
}
|
||||
}
|
||||
|
||||
public function resetRelationsData($sKey)
|
||||
{
|
||||
if (isset($this->aRelationsData[$sKey])) {
|
||||
unset($this->aRelationsData[$sKey]);
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
?>
|
||||
|
|
|
@ -10,10 +10,11 @@ class LS_ManyToManyRelation
|
|||
// Ссылка на $oEntityORM->aRelationsData[<relation_name>],
|
||||
// где relation_name - имя сязи, которую представляет объект
|
||||
protected $_aCollection = array();
|
||||
protected $bUpdated = false;
|
||||
|
||||
public function __construct(&$aCollection)
|
||||
public function __construct($aCollection)
|
||||
{
|
||||
$this->_aCollection = &$aCollection;
|
||||
$this->_aCollection = $aCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,6 +23,7 @@ class LS_ManyToManyRelation
|
|||
*/
|
||||
public function add($oEntity)
|
||||
{
|
||||
$this->bUpdated = true;
|
||||
$this->_aCollection[$oEntity->_getPrimaryKeyValue()] = $oEntity;
|
||||
}
|
||||
|
||||
|
@ -31,6 +33,7 @@ class LS_ManyToManyRelation
|
|||
*/
|
||||
public function delete($iId)
|
||||
{
|
||||
$this->bUpdated = true;
|
||||
if (is_array($iId)) {
|
||||
foreach ($iId as $id) {
|
||||
if (isset($this->_aCollection[$id])) {
|
||||
|
@ -41,6 +44,16 @@ class LS_ManyToManyRelation
|
|||
unset($this->_aCollection[$iId]);
|
||||
}
|
||||
}
|
||||
|
||||
public function getCollection()
|
||||
{
|
||||
return $this->_aCollection;
|
||||
}
|
||||
|
||||
public function isUpdated()
|
||||
{
|
||||
return $this->bUpdated;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -143,8 +143,8 @@ class MapperORM extends Mapper {
|
|||
|
||||
list($aFilterFields,$sFilterFields)=$this->BuildFilter($aFilter,$oEntitySample);
|
||||
|
||||
$sql = "SELECT count(*) as c FROM ?# a LEFT JOIN ".$sTableName." b ON b.?# = a.?# WHERE a.?#=? {$sFilterFields}";
|
||||
$aQueryParams=array_merge(array($sql,$aFilter['#join_table'],$sPrimaryKey,$aFilter['#relation_key'],$aFilter['#by_key'],$aFilter['#by_value']),array_values($aFilterFields));
|
||||
$sql = "SELECT count(*) as c FROM ?# a WHERE a.?#=? {$sFilterFields}";
|
||||
$aQueryParams=array_merge(array($sql,$aFilter['#join_table'],$aFilter['#by_key'],$aFilter['#by_value']),array_values($aFilterFields));
|
||||
|
||||
if($aRow=call_user_func_array(array($this->oDb,'selectRow'),$aQueryParams)) {
|
||||
return $aRow['c'];
|
||||
|
|
|
@ -20,18 +20,18 @@
|
|||
*
|
||||
*/
|
||||
abstract class ModuleORM extends Module {
|
||||
|
||||
|
||||
protected $oMapperORM=null;
|
||||
|
||||
|
||||
public function Init() {
|
||||
$this->_LoadMapperORM();
|
||||
}
|
||||
|
||||
|
||||
protected function _LoadMapperORM() {
|
||||
$this->oMapperORM=new MapperORM($this->oEngine->Database_GetConnect());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected function _AddEntity($oEntity) {
|
||||
$res=$this->oMapperORM->AddEntity($oEntity);
|
||||
// сбрасываем кеш
|
||||
|
@ -48,18 +48,18 @@ abstract class ModuleORM extends Module {
|
|||
// Обновление связей many_to_many
|
||||
$aRelationsData = $oEntity->_getRelationsData();
|
||||
foreach ($oEntity->_getRelations() as $sRelName => $aRelation) {
|
||||
if ($aRelation[0] == EntityORM::RELATION_TYPE_MANY_TO_MANY && isset($aRelationsData[$sRelName])) {
|
||||
if ($aRelation[0] == EntityORM::RELATION_TYPE_MANY_TO_MANY && $oEntity->$sRelName->isUpdated()) {
|
||||
// Сброс кэша по связям
|
||||
$this->Cache_Clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('m2m_'.$aRelation[2].$aRelation[4].$oEntity->_getPrimaryKeyValue()));
|
||||
|
||||
$this->_updateManyToManySet($aRelation, $aRelationsData[$sRelName], $oEntity->_getDataOne($oEntity->_getPrimaryKey()));
|
||||
$this->_updateManyToManySet($aRelation, $oEntity->$sRelName->getCollection(), $oEntity->_getDataOne($oEntity->_getPrimaryKey()));
|
||||
$oEntity->resetRelationsData($sRelName);
|
||||
}
|
||||
}
|
||||
return $oEntity;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
protected function _UpdateEntity($oEntity) {
|
||||
$res=$this->oMapperORM->UpdateEntity($oEntity);
|
||||
|
||||
|
@ -67,17 +67,19 @@ abstract class ModuleORM extends Module {
|
|||
// Обновление связей many_to_many
|
||||
$aRelationsData = $oEntity->_getRelationsData();
|
||||
foreach ($oEntity->_getRelations() as $sRelName => $aRelation) {
|
||||
if ($aRelation[0] == EntityORM::RELATION_TYPE_MANY_TO_MANY && isset($aRelationsData[$sRelName])) {
|
||||
if ($aRelation[0] == EntityORM::RELATION_TYPE_MANY_TO_MANY && $oEntity->$sRelName->isUpdated()) {
|
||||
// Сброс кэша по связям
|
||||
|
||||
$this->Cache_Clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('m2m_'.$aRelation[2].$aRelation[4].$oEntity->_getPrimaryKeyValue()));
|
||||
$this->_updateManyToManySet($aRelation, $aRelationsData[$sRelName], $oEntity->_getDataOne($oEntity->_getPrimaryKey()));
|
||||
$this->_updateManyToManySet($aRelation, $oEntity->$sRelName->getCollection(), $oEntity->_getDataOne($oEntity->_getPrimaryKey()));
|
||||
$oEntity->resetRelationsData($sRelName);
|
||||
}
|
||||
}
|
||||
// сбрасываем кеш
|
||||
$sEntity=$this->Plugin_GetRootDelegater('entity',get_class($oEntity));
|
||||
$this->Cache_Clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array($sEntity.'_save'));
|
||||
return $oEntity;
|
||||
}
|
||||
return $oEntity;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -87,8 +89,8 @@ abstract class ModuleORM extends Module {
|
|||
} else {
|
||||
return $this->_UpdateEntity($oEntity);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function _DeleteEntity($oEntity) {
|
||||
$res=$this->oMapperORM->DeleteEntity($oEntity);
|
||||
if ($res) {
|
||||
|
@ -106,8 +108,8 @@ abstract class ModuleORM extends Module {
|
|||
return $oEntity;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function _ReloadEntity($oEntity) {
|
||||
if($sPrimaryKey=$oEntity->_getPrimaryKey()) {
|
||||
if($sPrimaryKeyValue=$oEntity->_getDataOne($sPrimaryKey)) {
|
||||
|
@ -120,14 +122,14 @@ 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())) {
|
||||
$aRelationsData=$oEntity->_getRelationsData();
|
||||
|
@ -149,7 +151,7 @@ abstract class ModuleORM extends Module {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected function _GetParentOfEntity($oEntity) {
|
||||
if(in_array(EntityORM::RELATION_TYPE_TREE,$oEntity->_getRelations())) {
|
||||
$aRelationsData=$oEntity->_getRelationsData();
|
||||
|
@ -170,8 +172,8 @@ abstract class ModuleORM extends Module {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected function _GetAncestorsOfEntity($oEntity) {
|
||||
if(in_array(EntityORM::RELATION_TYPE_TREE,$oEntity->_getRelations())) {
|
||||
$aRelationsData=$oEntity->_getRelationsData();
|
||||
|
@ -191,9 +193,9 @@ abstract class ModuleORM extends Module {
|
|||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected function _GetDescendantsOfEntity($oEntity) {
|
||||
if(in_array(EntityORM::RELATION_TYPE_TREE,$oEntity->_getRelations())) {
|
||||
$aRelationsData=$oEntity->_getRelationsData();
|
||||
|
@ -215,7 +217,7 @@ abstract class ModuleORM extends Module {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function LoadTree($aFilter=array(),$sEntityFull=null) {
|
||||
if (is_null($sEntityFull)) {
|
||||
|
@ -252,23 +254,23 @@ abstract class ModuleORM extends Module {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function GetByFilter($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;
|
||||
}
|
||||
|
||||
|
||||
return $this->oMapperORM->GetByFilter($aFilter,$sEntityFull);
|
||||
}
|
||||
|
||||
|
||||
public function GetItemsByFilter($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;
|
||||
}
|
||||
}
|
||||
|
||||
// Если параметр #cache указан и пуст, значит игнорируем кэширование для запроса
|
||||
if (array_key_exists('#cache', $aFilter) && !$aFilter['#cache']) {
|
||||
|
@ -302,7 +304,7 @@ abstract class ModuleORM extends Module {
|
|||
$sRelType=$aRelations[$sRelationName][0];
|
||||
$sRelEntity=$this->Plugin_GetRootDelegater('entity',$aRelations[$sRelationName][1]); // получаем корневую сущность, без учета наследников
|
||||
$sRelKey=$aRelations[$sRelationName][2];
|
||||
|
||||
|
||||
if (!array_key_exists($sRelationName,$aRelations) or !in_array($sRelType,array(EntityORM::RELATION_TYPE_BELONGS_TO,EntityORM::RELATION_TYPE_HAS_ONE))) {
|
||||
throw new Exception("The entity <{$sEntityFull}> not have relation <{$sRelationName}>");
|
||||
}
|
||||
|
@ -314,7 +316,7 @@ abstract class ModuleORM extends Module {
|
|||
$aEntityKeys[$sRelKey][]=$oEntity->_getDataOne($sRelKey);
|
||||
}
|
||||
$aEntityKeys[$sRelKey]=array_unique($aEntityKeys[$sRelKey]);
|
||||
|
||||
|
||||
/**
|
||||
* Делаем общий запрос по всем ключам
|
||||
*/
|
||||
|
@ -322,9 +324,9 @@ abstract class ModuleORM extends Module {
|
|||
$sRelModuleName=Engine::GetModuleName($sRelEntity);
|
||||
$sRelEntityName=Engine::GetEntityName($sRelEntity);
|
||||
$sRelPluginPrefix=Engine::GetPluginPrefix($sRelEntity);
|
||||
$sRelPrimaryKey = method_exists($oRelEntityEmpty,'_getPrimaryKey') ? func_camelize($oRelEntityEmpty->_getPrimaryKey()) : 'Id';
|
||||
$sRelPrimaryKey = method_exists($oRelEntityEmpty,'_getPrimaryKey') ? func_camelize($oRelEntityEmpty->_getPrimaryKey()) : 'Id';
|
||||
$aRelData=Engine::GetInstance()->_CallModule("{$sRelPluginPrefix}{$sRelModuleName}_get{$sRelEntityName}ItemsByArray{$sRelPrimaryKey}", array($aEntityKeys[$sRelKey]));
|
||||
|
||||
|
||||
/**
|
||||
* Собираем набор
|
||||
*/
|
||||
|
@ -334,7 +336,7 @@ abstract class ModuleORM extends Module {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -343,14 +345,14 @@ abstract class ModuleORM extends Module {
|
|||
if (in_array('#index-from-primary', $aFilter) || !empty($aFilter['#index-from'])) {
|
||||
$aEntities = $this->_setIndexesFromField($aEntities, $aFilter);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Если запрашиваем постраничный список, то возвращаем сам список и общее количество записей
|
||||
*/
|
||||
if (isset($aFilter['#page'])) {
|
||||
return array('collection'=>$aEntities,'count'=>$this->GetCountItemsByFilter($aFilter,$sEntityFull));
|
||||
}
|
||||
|
||||
|
||||
return $aEntities;
|
||||
}
|
||||
|
||||
|
@ -371,7 +373,7 @@ abstract class ModuleORM extends Module {
|
|||
}
|
||||
return $aIndexedEntities;
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
|
@ -399,18 +401,18 @@ abstract class ModuleORM extends Module {
|
|||
|
||||
return $iCount;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Returns associative array of entities, indexed by PRIMARY KEY
|
||||
*/
|
||||
public function GetItemsByArray($aFilter,$sEntityFull=null) {
|
||||
foreach ($aFilter as $k=>$v) {
|
||||
$aFilter["{$k} IN"]=$v;
|
||||
unset($aFilter[$k]);
|
||||
public function GetItemsByArray($aFilter,$sEntityFull=null) {
|
||||
foreach ($aFilter as $k=>$v) {
|
||||
$aFilter["{$k} IN"]=$v;
|
||||
unset($aFilter[$k]);
|
||||
}
|
||||
$aFilter[] = '#index-from-primary';
|
||||
return $this->GetItemsByFilter($aFilter,$sEntityFull);
|
||||
}
|
||||
return $this->GetItemsByFilter($aFilter,$sEntityFull);
|
||||
}
|
||||
|
||||
public function GetItemsByJoinTable($aJoinData=array(),$sEntityFull=null) {
|
||||
if (is_null($sEntityFull)) {
|
||||
|
@ -483,24 +485,24 @@ abstract class ModuleORM extends Module {
|
|||
|
||||
return $iCount;
|
||||
}
|
||||
|
||||
|
||||
public function __call($sName,$aArgs) {
|
||||
if (preg_match("@^add([\w]+)$@i",$sName,$aMatch)) {
|
||||
return $this->_AddEntity($aArgs[0]);
|
||||
}
|
||||
|
||||
|
||||
if (preg_match("@^update([\w]+)$@i",$sName,$aMatch)) {
|
||||
return $this->_UpdateEntity($aArgs[0]);
|
||||
}
|
||||
|
||||
|
||||
if (preg_match("@^save([\w]+)$@i",$sName,$aMatch)) {
|
||||
return $this->_SaveEntity($aArgs[0]);
|
||||
}
|
||||
|
||||
|
||||
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]);
|
||||
}
|
||||
|
@ -512,26 +514,26 @@ abstract class ModuleORM extends Module {
|
|||
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;
|
||||
return $this->LoadTree($sEntityFull);
|
||||
}
|
||||
|
||||
|
||||
$sNameUnderscore=func_underscore($sName);
|
||||
$iEntityPosEnd=0;
|
||||
$iEntityPosEnd=0;
|
||||
if(strpos($sNameUnderscore,'_items')>=3) {
|
||||
$iEntityPosEnd=strpos($sNameUnderscore,'_items');
|
||||
} else if(strpos($sNameUnderscore,'_by')>=3) {
|
||||
|
@ -550,57 +552,57 @@ abstract class ModuleORM extends Module {
|
|||
$sNameUnderscore=substr_replace($sNameUnderscore,str_replace('_','',$sEntityName),4,$iEntityPosEnd-4);
|
||||
|
||||
$sEntityName=func_camelize($sEntityName);
|
||||
|
||||
|
||||
/**
|
||||
* getUserItemsByFilter() get_user_items_by_filter
|
||||
*/
|
||||
if (preg_match("@^get_([a-z]+)((_items)|())_by_filter$@i",$sNameUnderscore,$aMatch)) {
|
||||
if ($aMatch[2]=='_items') {
|
||||
return $this->GetItemsByFilter($aArgs[0],$sEntityName);
|
||||
} else {
|
||||
} else {
|
||||
return $this->GetByFilter($aArgs[0],$sEntityName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getUserItemsByArrayId() get_user_items_by_array_id
|
||||
*/
|
||||
if (preg_match("@^get_([a-z]+)_items_by_array_([_a-z]+)$@i",$sNameUnderscore,$aMatch)) {
|
||||
return $this->GetItemsByArray(array($aMatch[2]=>$aArgs[0]),$sEntityName);
|
||||
if (preg_match("@^get_([a-z]+)_items_by_array_([_a-z]+)$@i",$sNameUnderscore,$aMatch)) {
|
||||
return $this->GetItemsByArray(array($aMatch[2]=>$aArgs[0]),$sEntityName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getUserItemsByJoinTable() get_user_items_by_join_table
|
||||
*/
|
||||
if (preg_match("@^get_([a-z]+)_items_by_join_table$@i",$sNameUnderscore,$aMatch)) {
|
||||
return $this->GetItemsByJoinTable($aArgs[0],func_camelize($sEntityName));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getUserByLogin() get_user_by_login
|
||||
* getUserByLoginAndMail() get_user_by_login_and_mail
|
||||
* getUserItemsByName() get_user_items_by_name
|
||||
* getUserItemsByNameAndActive() get_user_items_by_name_and_active
|
||||
* getUserItemsByNameAndActive() get_user_items_by_name_and_active
|
||||
* getUserItemsByDateRegisterGte() get_user_items_by_date_register_gte (>=)
|
||||
* getUserItemsByProfileNameLike() get_user_items_by_profile_name_like
|
||||
* getUserItemsByCityIdIn() get_user_items_by_city_id_in
|
||||
*/
|
||||
*/
|
||||
if (preg_match("@^get_([a-z]+)((_items)|())_by_([_a-z]+)$@i",$sNameUnderscore,$aMatch)) {
|
||||
$aAliases = array( '_gte' => ' >=', '_lte' => ' <=', '_gt' => ' >', '_lt' => ' <', '_like' => ' LIKE', '_in' => ' IN' );
|
||||
$sSearchParams = str_replace(array_keys($aAliases),array_values($aAliases),$aMatch[5]);
|
||||
$aSearchParams=explode('_and_',$sSearchParams);
|
||||
$aSplit=array_chunk($aArgs,count($aSearchParams));
|
||||
$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') {
|
||||
return $this->GetItemsByFilter($aFilter,$sEntityName);
|
||||
} else {
|
||||
} else {
|
||||
return $this->GetByFilter($aFilter,$sEntityName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getUserAll() get_user_all OR
|
||||
* getUserItemsAll() get_user_items_all
|
||||
|
@ -614,14 +616,14 @@ abstract class ModuleORM extends Module {
|
|||
}
|
||||
return $this->GetItemsByFilter($aFilter,$sEntityName);
|
||||
}
|
||||
|
||||
|
||||
return $this->oEngine->_CallModule($sName,$aArgs);
|
||||
}
|
||||
|
||||
|
||||
static function buildTree($aItems,$aList=array(),$iLevel=0) {
|
||||
foreach($aItems as $oEntity) {
|
||||
$aChildren=$oEntity->getChildren();
|
||||
$bHasChildren = !empty($aChildren);
|
||||
$bHasChildren = !empty($aChildren);
|
||||
$sEntityId = $oEntity->_getDataOne($oEntity->_getPrimaryKey());
|
||||
$aList[$sEntityId] = array(
|
||||
'entity' => $oEntity,
|
||||
|
|
Loading…
Reference in a new issue