function addAkas(array $akas) { $itemName = Lms_Item::getItemName($this); $itemAkaName = $itemName . 'Aka'; $akas = array_unique($akas); //Для избежания дублирования ака foreach ($akas as $aka) { $akaObj = Lms_Item::create($itemAkaName); $akaObj->setName($aka); $this->add($akaObj); } }
/** * Возвращает элементы/аттрибуты указанные в пути xpath * * @param string $xpath * @return mixed */ public function getChilds($xpath) { if (!$xpath) { return $this; } //разбор запроса $xpaths = explode("/", $xpath, 2); $xpathCurrentStep = array_shift($xpaths); $xpathNextStep = count($xpaths) ? array_shift($xpaths) : null; $predicat = array(); if (preg_match('{^(.*?)\\[(.*?)\\]$}', $xpathCurrentStep, $matches)) { $xpathCurrentStep = $matches[1]; $predicat[] = $matches[2]; } //запрашивается поле if (0 === strpos($xpathCurrentStep, '@')) { $xpathCurrentStep = substr($xpathCurrentStep, 1); return call_user_func(array($this, "get{$xpathCurrentStep}")); } //запрашивается зависимый объект подразумевая использование линкатора $thisItemName = Lms_Item::getItemName($this); $relation = Lms_Item_Relations::get($thisItemName, $xpathCurrentStep); if (!$relation) { $linkator = Lms_Item::getLinkator($thisItemName, $xpathCurrentStep); return $this->getChilds("{$linkator}/{$xpath}"); } //запрашивается зависимый объект имеющий прямую связь $subTableName = Lms_Item::getTableName($xpathCurrentStep); $foreignKey = $relation['foreign_key']; $parentKeyValue = $this->__getValue($relation['parent_key']); Lms_Item::initStructure(Lms_Item::getClassName($xpathCurrentStep)); if (Lms_Item_Relations::ONE == $relation['type']) { //связь 1 к 1 if (Lms_Item::getSimplePk($xpathCurrentStep) == $foreignKey) { try { $item = Lms_Item::create($xpathCurrentStep, $parentKeyValue); return $item->getChilds($xpathNextStep); } catch (Lms_Item_RecordNotExistsException $e) { return null; } } else { $this->completeIndexValues($xpathCurrentStep, $foreignKey, $parentKeyValue); $subPkValues = Lms_Item_Store::getIndexedValues($subTableName, $foreignKey, $parentKeyValue); if (count($subPkValues)) { $subPkValue = reset($subPkValues); try { $item = Lms_Item::create($xpathCurrentStep, $subPkValue); return $item->getChilds($xpathNextStep); } catch (Lms_Item_RecordNotExistsException $e) { return null; } } } } else { //связь 1 ко многим $this->completeIndexValues($xpathCurrentStep, $foreignKey, $parentKeyValue); $result = array(); $subPkValues = Lms_Item_Store::getIndexedValues($subTableName, $foreignKey, $parentKeyValue); foreach ($subPkValues as $subPkValue) { $item = Lms_Item::create($xpathCurrentStep, $subPkValue); $result[] = $item->getChilds($xpathNextStep); } $this->performHandlers($result, $predicat); return $result; } }