/**
  * Save the date modified field
  * @param mixed $data the model data which is being saved
  * @param string $fieldName name of the field which has to be saved
  * @param int $id primary key to the data
  * @return null
  */
 public function processSaveField($data, $fieldName, $id)
 {
     if (!$id) {
         $id = $data->id;
     }
     $condition = new SimpleCondition(new FieldExpression(ModelTable::PRIMARY_KEY), new ScalarExpression($id));
     $statement = new UpdateStatement();
     $statement->addTable(new TableExpression($this->model->getName()));
     $statement->addValue(new FieldExpression(self::NAME), new ScalarExpression(time()));
     $statement->addCondition($condition);
     $connection = $this->model->getMeta()->getConnection();
     $connection->executeStatement($statement);
     $this->model->clearCache();
 }
 public function testUpdateStatement()
 {
     $condition = new SimpleCondition(new FieldExpression('id'), new ScalarExpression(1), '=');
     $statement = new UpdateStatement();
     $statement->addTable(new TableExpression('table'));
     $statement->addValue(new FieldExpression('field1'), new ScalarExpression('test'));
     $statement->addValue(new FieldExpression('field2'), new ScalarExpression(2));
     $statement->addValue(new FieldExpression('field3'), new ScalarExpression(false));
     $statement->addCondition($condition);
     $sql = $this->parser->parseStatement($statement);
     $this->assertNotNull($sql);
     $this->assertEquals('UPDATE `table` SET `field1` = \'test\', `field2` = 2, `field3` = 0 WHERE `id` = 1', $sql);
 }
 /**
  * Get the SQL of a update statement
  * @param zibo\library\database\manipulation\statement\manipulation\UpdateStatement $statement
  * @return string SQL of the update statement
  * @throws zibo\library\database\exception\DatabaseException when no table was added to the statement
  * @throws zibo\library\database\exception\DatabaseException when no values where added to the statement
  */
 protected function parseUpdateStatement(UpdateStatement $statement)
 {
     $tables = $statement->getTables();
     if (empty($tables)) {
         throw new DatabaseException('No tables added to the update statement');
     }
     $table = array_shift($tables);
     $values = $statement->getValues();
     if (empty($values)) {
         throw new DatabaseException('No values added to the update statement');
     }
     $sql = '';
     foreach ($values as $valueExpression) {
         $field = $valueExpression->getField();
         $value = $valueExpression->getValue();
         $value = $this->parseExpression($value);
         $sql .= ($sql ? ', ' : '') . $this->connection->quoteIdentifier($field->getName()) . ' = ' . $value;
     }
     $sql = 'UPDATE ' . $this->connection->quoteIdentifier($table->getName()) . ' SET ' . $sql;
     $conditions = $statement->getConditions();
     if ($conditions) {
         $operator = $statement->getOperator();
         $sql .= ' WHERE ' . $this->parseConditions($conditions, $operator, false);
     }
     return $sql;
 }
示例#4
0
 /**
  * Set the default site
  * @param int|Site $site
  * @return null
  * @throws Exception when an error occured
  */
 public function setDefaultSite($site)
 {
     $id = $this->getPrimaryKey($site);
     if ($site === $id) {
         $site = $this->findById($id, 0);
         if (!$site) {
             throw new ZiboException('Could not find site with id ' . $id);
         }
     }
     $transactionStarted = $this->startTransaction();
     try {
         $statement = new UpdateStatement();
         $statement->addTable(new TableExpression($this->meta->getName()));
         $statement->addValue(new FieldExpression('isDefault'), 0);
         $this->executeStatement($statement);
         $site->isDefault = true;
         $this->save($site, 'isDefault');
         $this->commitTransaction($transactionStarted);
     } catch (Exception $exception) {
         $this->rollbackTransaction($transactionStarted);
         throw $exception;
     }
 }
示例#5
0
 /**
  * Deletes or clears the data in the provided model which has links with the provided data
  * @param string $unlinkedModelName Name of the model which has links with this model but which are not linked from this model
  * @param mixed $data Data object
  * @return null
  */
 private function deleteDataInModel($unlinkedModelName, $data)
 {
     $model = $this->getModel($unlinkedModelName);
     $meta = $model->getMeta();
     $belongsTo = $meta->getBelongsTo();
     $fields = array();
     foreach ($belongsTo as $field) {
         if ($field->getRelationModelName() == $this->getName()) {
             $fields[] = $field->getName();
             break;
         }
     }
     if (!$fields) {
         return;
     }
     $deleteData = false;
     if (count($meta->getProperties()) == 1) {
         if (count($belongsTo) == 2) {
             $deleteData = true;
         }
     }
     $table = new TableExpression($unlinkedModelName);
     $id = new SqlExpression($data->id);
     if ($deleteData) {
         foreach ($fields as $fieldName) {
             $condition = new SimpleCondition(new FieldExpression($fieldName), $id, Condition::OPERATOR_EQUALS);
             $statement = new DeleteStatement();
             $statement->addTable($table);
             $statement->addCondition($condition);
             $this->executeStatement($statement);
         }
     } else {
         foreach ($fields as $fieldName) {
             $field = new FieldExpression($fieldName);
             $condition = new SimpleCondition($field, $id, Condition::OPERATOR_EQUALS);
             $statement = new UpdateStatement();
             $statement->addTable($table);
             $statement->addValue($field, null);
             $statement->addCondition($condition);
             $this->executeStatement($statement);
         }
     }
     $model->clearCache();
 }
示例#6
0
 /**
  * Copies a node
  * @param integer|Node $node Id of the node or the node to copy
  * @param boolean $recursive Set to true to also copy the children of the node
  * @param boolean $reorder Set to false to just copy the order index instead of adding the copied node after the source node
  * @param boolean $keepOriginalName Set to true to keep the name untouched, else a suffix like (copy) or (copy 2, 3 ...) will be added to the name of the copy
  * @param boolean $copyRoutes Set to true to copy the routes of the nodes. This will only work when copying a root node, else a validation error will occur
  * @param boolean $resetCopyTable Set to false for recursive copying
  * @param boolean $newParent Provide a new parent for the copy, needed for recursive copying
  * @return null
  */
 public function copy($node, $recursive = true, $reorder = true, $keepOriginalName = false, $copyRoutes = false, $resetCopyTable = true, $newParent = null)
 {
     $id = $this->getPrimaryKey($node);
     $node = $this->getNode($id, 1);
     if ($resetCopyTable) {
         $this->copyTable = array();
     }
     $copy = $this->createData();
     $nameSuffix = '';
     if (!$keepOriginalName) {
         $nameSuffix = $this->getNameSuffixForCopiedNode($node->parent, $node->name, $node->dataLocale);
     }
     $copy->name = $node->name . $nameSuffix;
     $copy->type = $node->type;
     if ($newParent) {
         $copy->parent = $newParent;
     } else {
         $copy->parent = $node->parent;
     }
     if ($reorder) {
         $copy->orderIndex = $node->orderIndex + 1;
     } else {
         $copy->orderIndex = $node->orderIndex;
     }
     if ($copyRoutes) {
         $copy->route = $node->route;
     }
     $isTransactionStarted = $this->startTransaction();
     try {
         $this->copyNodeSettings($node, $copy);
         $this->save($copy);
         $localizedModel = $this->meta->getLocalizedModel();
         $query = $localizedModel->createQuery();
         $query->addCondition('{dataId} = %1%', $node->id);
         $localizedNodes = $query->query();
         foreach ($localizedNodes as $localizedNode) {
             $nameSuffix = '';
             if (!$keepOriginalName) {
                 $nameSuffix = $this->getNameSuffixForCopiedNode($node->parent, $localizedNode->name, $localizedNode->dataLocale);
             }
             $copyLocalizedNode = $this->createData(false);
             $copyLocalizedNode->id = $copy->id;
             $copyLocalizedNode->name = $localizedNode->name . $nameSuffix;
             if ($copyRoutes) {
                 $copyLocalizedNode->route = $localizedNode->route;
             }
             $copyLocalizedNode->dataLocale = $localizedNode->dataLocale;
             $this->save($copyLocalizedNode);
         }
         if ($reorder) {
             $orderFieldExpression = new FieldExpression('orderIndex');
             $mathExpression = new MathematicalExpression();
             $mathExpression->addExpression($orderFieldExpression);
             $mathExpression->addExpression(new ScalarExpression(1));
             $idCondition = new SimpleCondition(new FieldExpression('id'), new ScalarExpression($copy->id), Condition::OPERATOR_NOT_EQUALS);
             $parentCondition = new SimpleCondition(new FieldExpression('parent'), new ScalarExpression($copy->parent), Condition::OPERATOR_EQUALS);
             $greaterCondition = new SimpleCondition($orderFieldExpression, new ScalarExpression($copy->orderIndex), Condition::OPERATOR_GREATER_OR_EQUALS);
             $condition = new NestedCondition();
             $condition->addCondition($idCondition);
             $condition->addCondition($parentCondition);
             $condition->addCondition($greaterCondition);
             $updateStatement = new UpdateStatement();
             $updateStatement->addTable(new TableExpression($this->getName()));
             $updateStatement->addValue($orderFieldExpression, $mathExpression);
             $updateStatement->addCondition($condition);
             $this->executeStatement($updateStatement);
         }
         if ($recursive) {
             $query = $this->createQuery();
             $query->setFields('{id}, {parent}, {orderIndex}');
             $query->addCondition('{parent} = \'' . $node->getPath() . '\'');
             // when copying a site, the path is numeric and won't be escaped by the orm
             $query->addOrderBy('{orderIndex} ASC');
             $children = $query->query();
             $path = $copy->getPath();
             foreach ($children as $child) {
                 $childCopy = $this->copy($child->id, true, false, true, $copyRoutes, false, $path);
             }
         }
         $this->copyTable[$id] = $copy->id;
         $this->commitTransaction($isTransactionStarted);
     } catch (Exception $exception) {
         $this->rollbackTransaction($isTransactionStarted);
         throw $exception;
     }
     return $copy;
 }