delete() public method

Deletes the record from the database.
public delete ( ) : integer | boolean
return integer | boolean the number of rows deleted, or `false` if the deletion is unsuccessful for some reason. Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.
示例#1
0
 /**
  * Delete slave record
  *
  * @param ActiveRecordInterface $slave
  */
 protected function deleteSlave(ActiveRecordInterface $slave)
 {
     if ($slave->delete() == false) {
         if (is_callable($this->errorDeleteCallback)) {
             call_user_func($this->errorDeleteCallback, $slave);
         }
     }
 }
示例#2
0
 /**
  * Destroys the relationship between two models.
  *
  * The model with the foreign key of the relationship will be deleted if `$delete` is true.
  * Otherwise, the foreign key will be set null and the model will be saved without validation.
  *
  * @param string $name the case sensitive name of the relationship.
  * @param ActiveRecordInterface $model the model to be unlinked from the current one.
  * You have to make sure that the model is really related with the current model as this method
  * does not check this.
  * @param boolean $delete whether to delete the model that contains the foreign key.
  * If false, the model's foreign key will be set null and saved.
  * If true, the model containing the foreign key will be deleted.
  * @throws InvalidCallException if the models cannot be unlinked
  */
 public function unlink($name, $model, $delete = false)
 {
     $relation = $this->getRelation($name);
     if ($relation->via !== null) {
         if (is_array($relation->via)) {
             /* @var $viaRelation ActiveQuery */
             list($viaName, $viaRelation) = $relation->via;
             $viaClass = $viaRelation->modelClass;
             unset($this->_related[$viaName]);
         } else {
             $viaRelation = $relation->via;
             $viaTable = reset($relation->via->from);
         }
         $columns = [];
         foreach ($viaRelation->link as $a => $b) {
             $columns[$a] = $this->{$b};
         }
         foreach ($relation->link as $a => $b) {
             $columns[$b] = $model->{$a};
         }
         $nulls = [];
         foreach (array_keys($columns) as $a) {
             $nulls[$a] = null;
         }
         if (is_array($relation->via)) {
             /* @var $viaClass ActiveRecordInterface */
             if ($delete) {
                 $viaClass::deleteAll($columns);
             } else {
                 $viaClass::updateAll($nulls, $columns);
             }
         } else {
             /* @var $viaTable string */
             /* @var $command Command */
             $command = static::getDb()->createCommand();
             if ($delete) {
                 $command->delete($viaTable, $columns)->execute();
             } else {
                 $command->update($viaTable, $nulls, $columns)->execute();
             }
         }
     } else {
         $p1 = $model->isPrimaryKey(array_keys($relation->link));
         $p2 = $this->isPrimaryKey(array_values($relation->link));
         if ($p2) {
             foreach ($relation->link as $a => $b) {
                 $model->{$a} = null;
             }
             $delete ? $model->delete() : $model->save(false);
         } elseif ($p1) {
             foreach ($relation->link as $a => $b) {
                 if (is_array($this->{$b})) {
                     // relation via array valued attribute
                     if (($key = array_search($model->{$a}, $this->{$b}, false)) !== false) {
                         $values = $this->{$b};
                         unset($values[$key]);
                         $this->{$b} = array_values($values);
                     }
                 } else {
                     $this->{$b} = null;
                 }
             }
             $delete ? $this->delete() : $this->save(false);
         } else {
             throw new InvalidCallException('Unable to unlink models: the link does not involve any primary key.');
         }
     }
     if (!$relation->multiple) {
         unset($this->_related[$name]);
     } elseif (isset($this->_related[$name])) {
         /* @var $b ActiveRecordInterface */
         foreach ($this->_related[$name] as $a => $b) {
             if ($model->getPrimaryKey() == $b->getPrimaryKey()) {
                 unset($this->_related[$name][$a]);
             }
         }
     }
 }
示例#3
0
 /**
  * @param ActiveRecordInterface $record
  */
 protected function deleteOrFail(ActiveRecordInterface $record)
 {
     if (!$record->delete()) {
         throw new InvalidValueException('No records were deleted from database.');
     }
 }