update() public méthode

For example, php $params = []; $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); The method will properly escape the table and column names.
public update ( string $table, array $columns, array | string $condition, array &$params ) : string
$table string the table to be updated.
$columns array the column data (name => value) to be updated.
$condition array | string the condition that will be put in the WHERE part. Please refer to [[Query::where()]] on how to specify condition.
$params array the binding parameters that will be modified by this method so that they can be bound to the DB command later.
Résultat string the UPDATE SQL
 /**
  * @inheritdoc
  */
 public function update($table, $columns, $condition, &$params)
 {
     $schema = $this->db->getSchema();
     if (($tableSchema = $schema->getTableSchema($table)) !== null) {
         $columnSchemas = $tableSchema->columns;
     } else {
         $columnSchemas = [];
     }
     foreach ($columns as $name => $value) {
         if ($value instanceof Expression) {
             $columns[$name] = $this->convertExpression($value);
         } elseif (isset($columnSchemas[$name]) && in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) {
             $columns[$name] = [$value, \PDO::PARAM_LOB];
         }
     }
     return parent::update($table, $columns, $condition, $params);
 }
 /**
  * @inheritdoc
  */
 public function update($table, $columns, $condition, &$params)
 {
     $schema = $this->db->getSchema();
     if (($tableSchema = $schema->getTableSchema($table)) !== null) {
         $columnSchemas = $tableSchema->columns;
     } else {
         $columnSchemas = [];
     }
     foreach ($columns as $name => $value) {
         if (in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) {
             $columns[$name] = [$value, 'blob'];
         }
     }
     return parent::update($table, $columns, $condition, $params);
 }
 /**
  * Recalculate sorting
  */
 public function recalculateSort()
 {
     $owner = $this->owner;
     $db = $this->owner->getDb();
     $builder = new QueryBuilder($db);
     $orderFields = ['sort' => 'asc'];
     foreach ($owner->primaryKey() as $field) {
         if ($field != 'sort') {
             $orderFields[$field] = 'asc';
         }
     }
     // recalculate sort
     $query = $builder->update($owner->tableName(), [$this->sortAttribute => new Expression('(@sortingCount:=(@sortingCount+1))')], $this->getCondition(), $params) . ' ' . $builder->buildOrderBy($orderFields);
     $db->createCommand('set @sortingCount=-1;' . $query, $params)->execute();
     // update in current record
     if (!$owner->getIsNewRecord()) {
         $owner->{$this->sortAttribute} = $owner->findOne($owner->getPrimaryKey())->{$this->sortAttribute};
     }
 }
Exemple #4
0
 /**
  * @inheritdoc
  */
 public function update($table, $columns, $condition, &$params)
 {
     return parent::update($table, $this->normalizeTableRowData($table, $columns), $condition, $params);
 }