save() public méthode

This method will call ActiveRecordInterface::insert when [[getIsNewRecord()|isNewRecord]] is true, or ActiveRecordInterface::update when [[getIsNewRecord()|isNewRecord]] is false. For example, to save a customer record: php $customer = new Customer; // or $customer = Customer::findOne($id); $customer->name = $name; $customer->email = $email; $customer->save();
public save ( boolean $runValidation = true, array $attributeNames = null ) : boolean
$runValidation boolean whether to perform validation (calling [[\yii\base\Model::validate()|validate()]]) before saving the record. Defaults to `true`. If the validation fails, the record will not be saved to the database and this method will return `false`.
$attributeNames array list of attribute names that need to be saved. Defaults to `null`, meaning all attributes that are loaded from DB will be saved.
Résultat boolean whether the saving succeeded (i.e. no validation errors occurred).
Exemple #1
0
 /**
  * @param array $link
  * @param ActiveRecordInterface $foreignModel
  * @param ActiveRecordInterface $primaryModel
  * @throws InvalidCallException
  */
 private function bindModels($link, $foreignModel, $primaryModel)
 {
     foreach ($link as $fk => $pk) {
         $value = $primaryModel->{$pk};
         if ($value === null) {
             throw new InvalidCallException('Unable to link models: the primary key of ' . get_class($primaryModel) . ' is null.');
         }
         if (is_array($foreignModel->{$fk})) {
             // relation via array valued attribute
             $foreignModel->{$fk} = array_merge($foreignModel->{$fk}, [$value]);
         } else {
             $foreignModel->{$fk} = $value;
         }
     }
     $foreignModel->save(false);
 }
 /**
  * @param array $link
  * @param ActiveRecordInterface $foreignModel
  * @param ActiveRecordInterface $primaryModel
  * @throws InvalidCallException
  */
 private function bindModels($link, $foreignModel, $primaryModel)
 {
     foreach ($link as $fk => $pk) {
         $value = $primaryModel->{$pk};
         if ($value === null) {
             throw new InvalidCallException('Unable to link models: the primary key of ' . get_class($primaryModel) . ' is null.');
         }
         $foreignModel->{$fk} = $value;
     }
     $foreignModel->save(false);
 }
 /**
  * @param ActiveRecordInterface $slave
  */
 protected function saveSlave(ActiveRecordInterface $slave)
 {
     if (!$slave->save()) {
         if ($this->errorSaveCallback !== null && is_callable([$this, 'errorSaveCallback'])) {
             call_user_func($this->errorSaveCallback, $slave);
         }
     }
 }
Exemple #4
0
 /**
  * @param ActiveRecordInterface $record
  * @param boolean $runValidation
  * @param array $attributeNames
  */
 protected function saveOrFail(ActiveRecordInterface $record, $runValidation = true, $attributeNames = null)
 {
     if (!$record->save($runValidation, $attributeNames)) {
         throw new InvalidValueException('Failed to save record into database.');
     }
 }