/**
  * Get the SQL of a delete statement
  * @param DeleteStatement $statement
  * @return string SQL of the delete statement
  * @throws zibo\library\database\exception\DatabaseException when no table was added to the statement
  */
 protected function parseDeleteStatement(DeleteStatement $statement)
 {
     $tables = $statement->getTables();
     if (empty($tables)) {
         throw new DatabaseException('No tables added to the insert statement');
     }
     $table = array_shift($tables);
     $sql = 'DELETE FROM ' . $this->connection->quoteIdentifier($table->getName());
     $conditions = $statement->getConditions();
     if ($conditions) {
         $operator = $statement->getOperator();
         $sql .= ' WHERE ' . $this->parseConditions($conditions, $operator, false);
     }
     return $sql;
 }
예제 #2
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();
 }