/** * Парсинг условий связи между моделями. * * Ничего сложного нет, просто определяются соответствия полей основной модели и модели из связи. Например: * * `FilmLinksTable::FILM_ID => FilmTable::ID (ref.FILM_ID => this.ID)` * * Или, например: * * `MediaTable::TYPE => 'FILM' (ref.TYPE => new DB\SqlExpression('?s', 'FILM'))` * * @param Entity\ReferenceField $reference Данные поля из getMap(). * * @return array Условия связи преобразованные в массив вида $conditions[$refField]['thisField' => $thisField, * 'customValue' => $customValue]. * $customValue - это результат парсинга SqlExpression. * Если шаблон SqlExpression не равен %s, то условие исключается из результата. */ protected function getReferenceConditions(Entity\ReferenceField $reference) { $conditionsFields = array(); foreach ($reference->getReference() as $leftCondition => $rightCondition) { $thisField = null; $refField = null; $customValue = null; // Поиск this.... в левом условии $thisFieldMatch = array(); $refFieldMatch = array(); if (preg_match('/=this\\.([A-z]+)/', $leftCondition, $thisFieldMatch) == 1) { $thisField = $thisFieldMatch[1]; } else { if (preg_match('/ref\\.([A-z]+)/', $leftCondition, $refFieldMatch) == 1) { $refField = $refFieldMatch[1]; } } // Поиск expression value... в правом условии $refFieldMatch = array(); if ($rightCondition instanceof \Bitrix\Main\DB\SqlExpression) { $customValueDirty = $rightCondition->compile(); $customValue = preg_replace('/^([\'"])(.+)\\1$/', '$2', $customValueDirty); if ($customValueDirty == $customValue) { // Если значение выражения не обрамлено кавычками, значит оно не нужно нам $customValue = null; } } else { if (preg_match('/ref\\.([A-z]+)/', $rightCondition, $refFieldMatch) > 0) { $refField = $refFieldMatch[1]; } } // Если не указано поле, которое нужно заполнить или не найдено содержимое для него, то исключаем условие if (empty($refField) || empty($thisField) && empty($customValue)) { continue; } else { $conditionsFields[$refField] = array('thisField' => $thisField, 'customValue' => $customValue); } } return $conditionsFields; }