Ejemplo n.º 1
0
 public function getEntityByPrimary(\Freetrix\Main\Entity\Base $entity, $primary, $select)
 {
     $this->connectInternal();
     $table = $entity->getDBTableName();
     $sqlConfiguration = $entity->getConnection()->getConfiguration();
     $primary = (array) $primary;
     if (count($primary) > 1) {
         throw new \Exception('HSPHP Read Socket doesn\'t support multiple select');
     }
     $indexId = $this->resource->getIndexId($sqlConfiguration['database'], $table, '', join(',', (array) $select));
     $this->resource->select($indexId, '=', $primary);
     $response = $this->resource->readResponse();
     //foreach
     $result = array();
     if (is_array($response)) {
         foreach ($response as $row) {
             $newRow = array();
             foreach ($row as $k => $v) {
                 $newRow[$select[$k]] = $v;
             }
             $result[] = $newRow;
         }
     }
     return $result;
 }
Ejemplo n.º 2
0
 /**
  * Returns entity object
  *
  * @return Base
  */
 public static function getEntity()
 {
     $class = get_called_class();
     if (!isset(static::$entity[$class])) {
         static::$entity[$class] = Base::getInstance($class);
     }
     return static::$entity[$class];
 }
Ejemplo n.º 3
0
 /**
  * @return callback[]|Validator\Base[]
  * @throws \Exception
  */
 public function getValidators()
 {
     if ($this->validators === null) {
         $validators = array();
         if ($this->validation !== null) {
             $validators = call_user_func($this->validation);
             if (!is_array($validators)) {
                 throw new \Exception(sprintf('Validation for %s field of %s entity should return array of validators', $this->name, $this->entity->getDataClass()));
             }
             foreach ($validators as $k => $validator) {
                 if (!$validator instanceof Validator\Base && !is_callable($validator)) {
                     throw new \Exception(sprintf('Validator "%s" of "%s" field of "%s" entity should be a Validator\\Base or callback', $k, $this->name, $this->entity->getDataClass()));
                 }
             }
         }
         $this->validators = $validators;
     }
     return $this->validators;
 }
Ejemplo n.º 4
0
 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 Freetrix namespace
     if ($class_path[0] === 'FREETRIX') {
         $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;
 }
Ejemplo n.º 5
0
 protected function buildJoinMap()
 {
     $connection = \Freetrix\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;
         }
     }
 }