예제 #1
0
 /**
  * Queries for has one relation fields with a link model to self
  * @param ModelQuery $query Query for the has many data
  * @param string $fieldName Name of the field which will contain the has many data
  * @param array $foreignKeys Array with ModelField objects
  * @param integer $id Primary key of the data which will contain the has many data
  * @return array Model query result for the has many field
  */
 private function queryHasManyWithLinkModelToSelf(ModelQuery $query, ModelMeta $meta, $fieldName, array $foreignKeys, $id)
 {
     $order = $meta->getRelationOrder($fieldName);
     if ($order != null) {
         $query->addOrder($order);
     }
     $result = array();
     $queryResult = $query->query();
     foreach ($queryResult as $data) {
         foreach ($foreignKeys as $foreignKey) {
             $foreignKey = $foreignKey->getName();
             if ($data->{$foreignKey}->id != $id) {
                 break;
             }
         }
         $result[$data->{$foreignKey}->id] = $data->{$key};
     }
     return $result;
 }
예제 #2
0
 /**
  * Sets the model query to this parser and initializes the parser for this model query.
  * @param zibo\library\orm\query\ModelQuery $modelQuery
  * @return null
  */
 private function setModelQuery(ModelQuery $modelQuery)
 {
     $this->meta = $modelQuery->getModel()->getMeta();
     $this->recursiveDepth = $modelQuery->getRecursiveDepth();
     $this->localize = false;
     $this->locale = $modelQuery->getLocale();
     $this->includeUnlocalizedData = $modelQuery->willIncludeUnlocalizedData();
     $this->addIsLocalizedOrder = $modelQuery->willAddIsLocalizedOrder();
     $this->fields = array();
     $this->recursiveBelongsToFields = array();
     $this->recursiveHasFields = array();
     $this->tables = array();
     $this->fieldJoins = array();
     $this->conditionJoins = array();
     $this->addTable($this->meta->getName(), self::ALIAS_SELF);
     $this->addTable($this->meta->getName() . LocalizedModel::MODEL_SUFFIX, self::ALIAS_SELF_LOCALIZED);
     $this->statement = new SelectStatement();
     $this->statement->addTable($this->tables[self::ALIAS_SELF]);
     $joins = $modelQuery->getJoins();
     foreach ($joins as $join) {
         $table = $join->getTable();
         $this->addTable($table->getModelName(), $table->getAlias());
     }
 }
예제 #3
0
 /**
  * Adds a order by to this query with named variables
  * @param string $expression Order by expression
  * @param array $variables Array with the variable name as key and the variable as value
  * @return null
  */
 public function addOrderByWithVariables($expression, array $variables)
 {
     $expression = $this->parseVariables($expression, $variables);
     parent::addOrderByWithVariables($expression, array());
 }
예제 #4
0
 /**
  * Gets the data object for the logs in the provided query
  * @param string $modelName Name of the data model
  * @param int $id Primary key of the data
  * @param zibo\library\orm\query\ModelQuery $query of the data logs
  * @return mixed Data
  */
 protected function getDataByQuery($modelName, $id, ModelQuery $query, $recursiveDepth)
 {
     $dataModel = $this->getModel($modelName);
     $dataMeta = $dataModel->getMeta();
     if (!$dataMeta->isLogged()) {
         return $dataModel->findById($id);
     }
     $logs = $query->query();
     if (!$logs) {
         throw new ZiboException('No logs for ' . $modelName . ' ' . $id);
     }
     $dataDate = 0;
     $dataFields = array();
     foreach ($logs as $log) {
         foreach ($log->changes as $change) {
             $dataFields[$change->fieldName] = $change->newValue;
         }
         $dataDate = $log->dateAdded;
     }
     $data = $dataModel->createData(false);
     $data->id = $id;
     foreach ($dataFields as $fieldName => $value) {
         $data->{$fieldName} = $value;
         if (!$dataMeta->hasField($fieldName)) {
             continue;
         }
         $field = $dataMeta->getField($fieldName);
         if (!$field instanceof RelationField) {
             continue;
         }
         $fieldModelName = $field->getRelationModelName();
         if ($field instanceof HasManyField) {
             if (!$data->{$fieldName}) {
                 $data->{$fieldName} = array();
             } else {
                 $ids = explode(self::VALUE_SEPARATOR, $data->{$fieldName});
                 $values = array();
                 foreach ($ids as $id) {
                     $id = trim($id);
                     if (!$id) {
                         continue;
                     }
                     if ($recursiveDepth) {
                         $values[$id] = $this->getDataByDate($fieldModelName, $id, $dataDate, $recursiveDepth - 1);
                     } else {
                         $values[$id] = $id;
                     }
                 }
                 $data->{$fieldName} = $values;
             }
             continue;
         }
         if (!$data->{$fieldName}) {
             $data->{$fieldName} = null;
         } else {
             if ($recursiveDepth) {
                 $data->{$fieldName} = $this->getDataByDate($fieldModelName, $data->{$fieldName}, $dataDate, $recursiveDepth - 1);
             }
         }
     }
     if ($dataMeta->isLocalized()) {
         $locale = $query->getLocale();
         $localizedModel = $dataMeta->getLocalizedModel();
         $localizedId = $localizedModel->getLocalizedId($data->id, $locale);
         if ($localizedId) {
             try {
                 $localizedData = $this->getDataByDate($localizedModel->getName(), $localizedId, $dataDate, $recursiveDepth);
                 $localizedFields = $dataMeta->getLocalizedFields();
                 foreach ($localizedFields as $fieldName => $field) {
                     if (isset($localizedData->{$fieldName})) {
                         $data->{$fieldName} = $localizedData->{$fieldName};
                     }
                 }
                 $data->dataLocale = $locale;
             } catch (Exception $exception) {
                 Zibo::getInstance()->runEvent(Zibo::EVENT_LOG, $exception->getMessage(), $exception->getTraceAsString(), 1);
             }
         }
     }
     return $data;
 }