/** * Получить список сущностей по фильтру * * @param array $aFilter Фильтр * @param string|null $sEntityFull Название класса сущности * * @return array * @throws Exception */ public function GetItemsByFilter($aFilter = array(), $sEntityFull = null) { if (is_null($aFilter)) { $aFilter = array(); } if (is_null($sEntityFull)) { $sEntityFull = E::GetPluginPrefix($this) . 'Module' . E::GetModuleName($this) . '_Entity' . E::GetModuleName(get_class($this)); } elseif (!substr_count($sEntityFull, '_')) { $sEntityFull = E::GetPluginPrefix($this) . 'Module' . E::GetModuleName($this) . '_Entity' . $sEntityFull; } elseif (strpos($sEntityFull, '_') && strpos($sEntityFull, 'Module') !== 0 && !strpos($sEntityFull, '_Entity')) { if (substr_count($sEntityFull, '_') == 1) { list($sModule, $sEntity) = explode('_', $sEntityFull); $sEntityFull = E::GetPluginPrefix($this) . 'Module' . $sModule . '_Entity' . $sEntity; } } // Если параметр #cache указан и пуст, значит игнорируем кэширование для запроса if (array_key_exists('#cache', $aFilter) && !$aFilter['#cache']) { $aEntities = $this->oMapper->GetItemsByFilter($aFilter, $sEntityFull); } else { $sEntityFullRoot = E::ModulePlugin()->GetRootDelegater('entity', $sEntityFull); $sCacheKey = $sEntityFullRoot . '_items_by_filter_' . serialize($aFilter); $aCacheTags = array($sEntityFullRoot . '_save', $sEntityFullRoot . '_delete'); $iCacheTime = 60 * 60 * 24; // скорее лучше хранить в свойстве сущности, для возможности выборочного переопределения // переопределяем из параметров if (isset($aFilter['#cache'][0])) { $sCacheKey = $aFilter['#cache'][0]; } if (isset($aFilter['#cache'][1])) { $aCacheTags = $aFilter['#cache'][1]; } if (isset($aFilter['#cache'][2])) { $iCacheTime = $aFilter['#cache'][2]; } if (false === ($aEntities = E::ModuleCache()->Get($sCacheKey))) { $aEntities = $this->oMapper->GetItemsByFilter($aFilter, $sEntityFull); E::ModuleCache()->Set($aEntities, $sCacheKey, $aCacheTags, $iCacheTime); } } /** * Если необходимо подцепить связанные данные */ if (count($aEntities) && isset($aFilter['#with'])) { if (!is_array($aFilter['#with'])) { $aFilter['#with'] = array($aFilter['#with']); } $oEntityEmpty = E::GetEntity($sEntityFull); $aRelations = $oEntityEmpty->_getRelations(); $aEntityKeys = array(); foreach ($aFilter['#with'] as $sRelationName) { $sRelType = $aRelations[$sRelationName][0]; // получаем корневую сущность, без учета наследников $sRelEntity = E::ModulePlugin()->GetRootDelegater('entity', $aRelations[$sRelationName][1]); $sRelKey = $aRelations[$sRelationName][2]; if (!array_key_exists($sRelationName, $aRelations) || !in_array($sRelType, array(EntityORM::RELATION_TYPE_BELONGS_TO, EntityORM::RELATION_TYPE_HAS_ONE))) { throw new Exception("The entity <{$sEntityFull}> not have relation <{$sRelationName}>"); } /** * Формируем список ключей */ foreach ($aEntities as $oEntity) { $aEntityKeys[$sRelKey][] = $oEntity->getProp($sRelKey); } $aEntityKeys[$sRelKey] = array_unique($aEntityKeys[$sRelKey]); /** * Делаем общий запрос по всем ключам */ $oRelEntityEmpty = E::GetEntity($sRelEntity); $sRelModuleName = E::GetModuleName($sRelEntity); $sRelEntityName = E::GetEntityName($sRelEntity); $sRelPluginPrefix = E::GetPluginPrefix($sRelEntity); $sRelPrimaryKey = method_exists($oRelEntityEmpty, '_getPrimaryKey') ? F::StrCamelize($oRelEntityEmpty->_getPrimaryKey()) : 'Id'; $aCallParams = array($aEntityKeys[$sRelKey]); $aRelData = E::GetInstance()->_CallModule("{$sRelPluginPrefix}{$sRelModuleName}_get{$sRelEntityName}ItemsByArray{$sRelPrimaryKey}", $aCallParams); /** * Собираем набор */ foreach ($aEntities as $oEntity) { if (isset($aRelData[$oEntity->getProp($sRelKey)])) { $oEntity->_setData(array($sRelationName => $aRelData[$oEntity->getProp($sRelKey)])); } } } } /** * Returns assotiative array, indexed by PRIMARY KEY or another field. */ 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; }
/** * Ставим хук на вызов неизвестного метода и считаем что хотели вызвать метод какого либо модуля * Также производит обработку методов set* и get* * Учитывает связи и может возвращать связанные данные * * @see Engine::_CallModule * * @param string $sName Имя метода * @param array $aArgs Аргументы * * @return mixed */ public function __call($sName, $aArgs) { $sType = substr($sName, 0, strpos(F::StrUnderscore($sName), '_')); if (!strpos($sName, '_') && in_array($sType, array('get', 'set', 'reload'))) { $sKey = F::StrUnderscore(preg_replace('/' . $sType . '/', '', $sName, 1)); if ($sType == 'get') { if (isset($this->_aData[$sKey])) { return $this->_aData[$sKey]; } else { $sField = $this->_getField($sKey); if ($sField != $sKey && isset($this->_aData[$sField])) { return $this->_aData[$sField]; } } /** * Проверяем на связи */ if (array_key_exists($sKey, $this->aRelations)) { $sEntityRel = $this->aRelations[$sKey][1]; $sRelationType = $this->aRelations[$sKey][0]; $sRelationKey = $this->aRelations[$sKey][2]; $sRelationJoinTable = null; $sRelationJoinTableKey = 0; // foreign key в join-таблице для текущей сущности if ($sRelationType == self::RELATION_TYPE_MANY_TO_MANY && array_key_exists(3, $this->aRelations[$sKey])) { $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 = E::GetModuleName($sEntityRel); $sRelEntityName = E::GetEntityName($sEntityRel); $sRelPluginPrefix = E::GetPluginPrefix($sEntityRel); $sRelPrimaryKey = 'id'; if ($oRelEntity = E::GetEntity($sEntityRel)) { $sRelPrimaryKey = $oRelEntity->_getPrimaryKey(); } $iPrimaryKeyValue = $this->getProp($this->_getPrimaryKey()); $sCmd = ''; $mCmdArgs = array(); switch ($sRelationType) { case self::RELATION_TYPE_BELONGS_TO: $sCmd = "{$sRelPluginPrefix}{$sRelModuleName}_get{$sRelEntityName}By" . F::StrCamelize($sRelPrimaryKey); $mCmdArgs = $this->getProp($sRelationKey); break; case self::RELATION_TYPE_HAS_ONE: $sCmd = "{$sRelPluginPrefix}{$sRelModuleName}_get{$sRelEntityName}By" . F::StrCamelize($sRelationKey); $mCmdArgs = $iPrimaryKeyValue; break; case self::RELATION_TYPE_HAS_MANY: $sCmd = "{$sRelPluginPrefix}{$sRelModuleName}_get{$sRelEntityName}ItemsByFilter"; $mCmdArgs = array($sRelationKey => $iPrimaryKeyValue); break; case self::RELATION_TYPE_MANY_TO_MANY: $sCmd = "{$sRelPluginPrefix}Module{$sRelModuleName}_get{$sRelEntityName}ItemsByJoinTable"; $mCmdArgs = array('#join_table' => Config::Get($sRelationJoinTable), '#relation_key' => $sRelationKey, '#by_key' => $sRelationJoinTableKey, '#by_value' => $iPrimaryKeyValue, '#index-from-primary' => true); break; default: break; } // Нужно ли учитывать дополнительный фильтр $bUseFilter = is_array($mCmdArgs) && array_key_exists(0, $aArgs) && is_array($aArgs[0]); if ($bUseFilter) { $mCmdArgs = array_merge($mCmdArgs, $aArgs[0]); } $aCallArgs = array($mCmdArgs); $res = E::GetInstance()->_CallModule($sCmd, $aCallArgs); // Сохраняем данные только в случае "чистой" выборки if (!$bUseFilter) { $this->aRelationsData[$sKey] = $res; } // Создаём объекты-обёртки для связей MANY_TO_MANY if ($sRelationType == self::RELATION_TYPE_MANY_TO_MANY) { $this->_aManyToManyRelations[$sKey] = new LS_ManyToManyRelation($res); } return $res; } return null; } elseif ($sType == 'set' && array_key_exists(0, $aArgs)) { if (array_key_exists($sKey, $this->aRelations)) { $this->aRelationsData[$sKey] = $aArgs[0]; } else { $this->_aData[$this->_getField($sKey)] = $aArgs[0]; } } elseif ($sType == 'reload') { if (array_key_exists($sKey, $this->aRelationsData)) { unset($this->aRelationsData[$sKey]); return $this->__call('get' . F::StrCamelize($sKey), $aArgs); } } } else { return E::getInstance()->_CallModule($sName, $aArgs); } }