public function getCode() { $code = ''; // get absolute path to class $class_path = explode('\\', strtoupper(ltrim($this->className, '\\'))); // cut class name to leave namespace only $class_path = array_slice($class_path, 0, -1); // cut Bitrix namespace if ($class_path[0] === 'BITRIX') { $class_path = array_slice($class_path, 1); } // glue module name if (count($class_path)) { $code = join('_', $class_path) . '_'; } // glue entity name $code .= strtoupper(Base::camel2snake($this->getName())); return $code; }
protected function buildJoinMap($chains = null) { $connection = Main\Application::getConnection(); $helper = $connection->getSqlHelper(); $aliasLength = $helper->getAliasLength(); $talias_count = 0; if (empty($chains)) { $chains = $this->global_chains; } foreach ($chains as $chain) { if ($chain->getLastElement()->getParameter('talias')) { // already been here continue; } // in NO_DOUBLING mode skip 1:N relations that presented in filter only if ($chain->forcesDataDoublingOff() || $this->data_doubling_off && $chain->hasBackReference()) { $alias = $chain->getAlias(); if (isset($this->filter_chains[$alias]) && !isset($this->select_chains[$alias]) && !isset($this->select_expr_chains[$alias]) && !isset($this->group_chains[$alias]) && !isset($this->order_chains[$alias])) { continue; } } $prev_alias = $this->getInitAlias(false); $map_key = ''; /** * elemenets after init entity * @var $elements QueryChainElement[] * */ $elements = array_slice($chain->getAllElements(), 1); $currentDedinition = array(); foreach ($elements as $element) { $table_alias = null; /** * define main objects * @var $ref_field ReferenceField * @var $dst_entity Base */ if ($element->getValue() instanceof ReferenceField) { // ref to another entity $ref_field = $element->getValue(); $dst_entity = $ref_field->getRefEntity(); } elseif (is_array($element->getValue())) { // link from another entity to this list($dst_entity, $ref_field) = $element->getValue(); } else { // scalar field $element->setParameter('talias', $prev_alias . $this->table_alias_postfix); continue; } // mapping if (empty($map_key)) { $map_key = join('.', $currentDedinition); } $map_key .= '/' . $ref_field->getName() . '/' . $dst_entity->getName(); $currentDedinition[] = $element->getDefinitionFragment(); if (isset($this->join_registry[$map_key])) { // already connected $table_alias = $this->join_registry[$map_key]; } else { // prepare reference $reference = $ref_field->getReference(); if ($element->getValue() instanceof ReferenceField) { // ref to another entity if (is_null($table_alias)) { $table_alias = $prev_alias . '_' . strtolower($ref_field->getName()); if (strlen($table_alias . $this->table_alias_postfix) > $aliasLength) { $table_alias = 'TALIAS_' . ++$talias_count; } } $alias_this = $prev_alias; $alias_ref = $table_alias; $isBackReference = false; $definition_this = join('.', array_slice($currentDedinition, 0, -1)); $definition_ref = join('.', $currentDedinition); } elseif (is_array($element->getValue())) { if (is_null($table_alias)) { $table_alias = Base::camel2snake($dst_entity->getName()) . '_' . strtolower($ref_field->getName()); $table_alias = $prev_alias . '_' . $table_alias; if (strlen($table_alias . $this->table_alias_postfix) > $aliasLength) { $table_alias = 'TALIAS_' . ++$talias_count; } } $alias_this = $table_alias; $alias_ref = $prev_alias; $isBackReference = true; $definition_this = join('.', $currentDedinition); $definition_ref = join('.', array_slice($currentDedinition, 0, -1)); } else { throw new Main\SystemException(sprintf('Unknown reference element `%s`', $element->getValue())); } // replace this. and ref. to real definition $csw_reference = $this->prepareJoinReference($reference, $alias_this . $this->table_alias_postfix, $alias_ref . $this->table_alias_postfix, $definition_this, $definition_ref, $isBackReference); // double check after recursive call in prepareJoinReference if (!isset($this->join_registry[$map_key])) { $join = array('type' => $ref_field->getJoinType(), 'table' => $dst_entity->getDBTableName(), 'alias' => $table_alias . $this->table_alias_postfix, 'reference' => $csw_reference, 'map_key' => $map_key); $this->join_map[] = $join; $this->join_registry[$map_key] = $table_alias; } } // set alias for each element $element->setParameter('talias', $table_alias . $this->table_alias_postfix); $prev_alias = $table_alias; } } }
protected function buildJoinMap() { $connection = \Bitrix\Main\Application::getConnection(); $helper = $connection->getSqlHelper(); $aliasLength = $helper->getAliasLength(); // list of used joins $done = array(); $talias_count = 0; foreach ($this->global_chains as $chain) { if ($chain->getLastElement()->getParameter('talias')) { // already been here continue; } // in NO_DOUBLING mode skip 1:N relations that presented in filter only if (!$this->data_doubling && $chain->hasBackReference()) { $alias = $chain->getAlias(); if (isset($this->filter_chains[$alias]) && !isset($this->select_chains[$alias]) && !isset($this->select_expr_chains[$alias]) && !isset($this->group_chains[$alias]) && !isset($this->order_chains[$alias])) { continue; } } $prev_entity = $this->init_entity; $prev_alias = $this->getInitAlias(false); $map_key = ''; /** * elemenets after init entity * @var $elements QueryChainElement[] * */ $elements = array_slice($chain->getAllElements(), 1); foreach ($elements as $element) { $table_alias = null; /** * define main objects * @var $src_entity Base * @var $ref_field ReferenceField * @var $dst_entity Base */ if ($element->getValue() instanceof ReferenceField) { // ref to another entity $src_entity = $prev_entity; $ref_field = $element->getValue(); $dst_entity = $ref_field->getRefEntity(); } elseif (is_array($element->getValue())) { // link from another entity to this $src_entity = $prev_entity; list($dst_entity, $ref_field) = $element->getValue(); } else { // scalar field $element->setParameter('talias', $prev_alias . $this->table_alias_postfix); continue; } // mapping if (empty($map_key)) { $map_key = $src_entity->getName(); } $map_key .= '/' . $ref_field->getName() . '/' . $dst_entity->getName(); if (isset($done[$map_key])) { // already connected $table_alias = $done[$map_key]; } else { // prepare reference $reference = $ref_field->getReference(); if ($element->getValue() instanceof ReferenceField) { // ref to another entity if (is_null($table_alias)) { $table_alias = $prev_alias . '_' . strtolower($ref_field->getName()); if (strlen($table_alias . $this->table_alias_postfix) > $aliasLength) { $table_alias = 'TALIAS' . $this->table_alias_postfix . '_' . ++$talias_count; } } $alias_this = $prev_alias; $alias_ref = $table_alias; } elseif (is_array($element->getValue())) { if (is_null($table_alias)) { $table_alias = Base::camel2snake($dst_entity->getName()) . '_' . strtolower($ref_field->getName()); $table_alias = $prev_alias . '_' . $table_alias; if (strlen($table_alias . $this->table_alias_postfix) > $aliasLength) { $table_alias = 'TALIAS' . $this->table_alias_postfix . '_' . ++$talias_count; } } $alias_this = $table_alias; $alias_ref = $prev_alias; if ($dst_entity->isUtm()) { // add to $reference $reference = array($reference, '=this.FIELD_ID' => array('?i', $element->getParameter('ufield')->getFieldId())); } } // replace this. and ref. to real definition -- not supported yet // instead it we set $alias_this and $alias_ref $csw_reference = $this->prepareJoinReference($reference, $alias_this . $this->table_alias_postfix, $alias_ref . $this->table_alias_postfix); $join = array('type' => $ref_field->getJoinType(), 'table' => $dst_entity->getDBTableName(), 'alias' => $table_alias . $this->table_alias_postfix, 'reference' => $csw_reference); $this->join_map[] = $join; $done[$map_key] = $table_alias; } // set alias for each element $element->setParameter('talias', $table_alias . $this->table_alias_postfix); $prev_entity = $dst_entity; $prev_alias = $table_alias; } } }