public function insert($runValidation = true, $attributes = null)
    {
        $validationPromise = $runValidation
            ? new LazyPromise(function() use ($attributes) {
                return $this->validate($attributes);
            })
            : Reaction\Promise\resolveLazy(true);
        return $validationPromise
            ->otherwiseLazy(function() {
                Reaction::info('Model not inserted due to validation error in {method}.', ['method' => __METHOD__]);
                return reject(new ValidationError("Model validation error"));
            })->thenLazy(function() use ($attributes) {
                if (!$this->isTransactional(self::OP_INSERT)) {
                    return new LazyPromise(function() use ($attributes) {
                        return $this->insertInternal($attributes);
                    });
                }

                $transaction = static::getDb()->createTransaction();
                return $transaction->begin()->then(
                    function(ConnectionInterface $connection) use ($attributes) {
                        return $this->insertInternal($attributes, $connection);
                    }
                )->then(
                    function($result) use ($transaction) {
                        return $transaction->commit()->then(function() use ($result) {
                            return $result;
                        });
                    },
                    function($exception) use ($transaction) {
                        return $transaction->rollBack()->then(
                            function() use ($exception) { throw $exception; },
                            function($e) use ($exception) { throw $exception; }
                        );
                    }
                );
            });
    }
public function insert($runValidation = true, $attributes = null)
    {
        $validationPromise = $runValidation
            ? new LazyPromise(function() use ($attributes) {
                return $this->validate($attributes);
            })
            : Reaction\Promise\resolveLazy(true);
        return $validationPromise
            ->otherwiseLazy(function() {
                Reaction::info('Model not inserted due to validation error in {method}.', ['method' => __METHOD__]);
                return reject(new ValidationError("Model validation error"));
            })->thenLazy(function() use ($attributes) {
                if (!$this->isTransactional(self::OP_INSERT)) {
                    return new LazyPromise(function() use ($attributes) {
                        return $this->insertInternal($attributes);
                    });
                }

                $transaction = static::getDb()->createTransaction();
                return $transaction->begin()->then(
                    function(ConnectionInterface $connection) use ($attributes) {
                        return $this->insertInternal($attributes, $connection);
                    }
                )->then(
                    function($result) use ($transaction) {
                        return $transaction->commit()->then(function() use ($result) {
                            return $result;
                        });
                    },
                    function($exception) use ($transaction) {
                        return $transaction->rollBack()->then(
                            function() use ($exception) { throw $exception; },
                            function($e) use ($exception) { throw $exception; }
                        );
                    }
                );
            });
    }
public function insert ( $ runValidation = true , $ attributes = null ) { $ validationPromise = $ runValidation ? new LazyPromise ( function ( ) use ( $ attributes ) { return $ this -> validate ( $ attributes ) ; } ) : Reaction \ Promise \ resolveLazy ( true ) ; return $ validationPromise -> otherwiseLazy ( function ( ) { Reaction :: info ( 'Model not inserted due to validation error in {method}.' , [ 'method' => __METHOD__ ] ) ; return reject ( new ValidationError ( "Model validation error" ) ) ; } ) -> thenLazy ( function ( ) use ( $ attributes ) { if ( ! $ this -> isTransactional ( self :: OP_INSERT ) ) { return new LazyPromise ( function ( ) use ( $ attributes ) { return $ this -> insertInternal ( $ attributes ) ; } ) ; } $ transaction = static :: getDb ( ) -> createTransaction ( ) ; return $ transaction -> begin ( ) -> then ( function ( ConnectionInterface $ connection ) use ( $ attributes ) { return $ this -> insertInternal ( $ attributes , $ connection ) ; } ) -> then ( function ( $ result ) use ( $ transaction ) { return $ transaction -> commit ( ) -> then ( function ( ) use ( $ result ) { return $ result ; } ) ; } , function ( $ exception ) use ( $ transaction ) { return $ transaction -> rollBack ( ) -> then ( function ( ) use ( $ exception ) { throw $ exception ; } , function ( $ e ) use ( $ exception ) { throw $ exception ; } ) ; } ) ; } ) ; }
Inserts a row into the associated database table using the attribute values of this record.

This method performs the following steps in order:

1. call [[beforeValidate()]] when `$runValidation` is `true`. If [[beforeValidate()]]
returns `false`, the rest of the steps will be skipped;
2. call [[afterValidate()]] when `$runValidation` is `true`. If validation
failed, the rest of the steps will be skipped;
3. call [[beforeSave()]]. If [[beforeSave()]] returns `false`,
the rest of the steps will be skipped;
4. insert the record into database. If this fails, it will skip the rest of the steps;
5. call [[afterSave()]];

In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]],
[[EVENT_AFTER_VALIDATE]], [[EVENT_BEFORE_INSERT]], and [[EVENT_AFTER_INSERT]]
will be raised by the corresponding methods.

Only the [[dirtyAttributes|changed attribute values]] will be inserted into database.

If the table's primary key is auto-incremental and is `null` during insertion,
it will be populated with the actual value after insertion.

For example, to insert a customer record:

```php
$customer = new Customer;
$customer->name = $name;
$customer->email = $email;
$customer->insert();
```

@param bool $runValidation whether to perform validation (calling [[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`.
@param array $attributes list of attributes that need to be saved. Defaults to `null`,
meaning all attributes that are loaded from DB will be saved.
@return LazyPromiseInterface with bool whether the attributes are valid and the record is inserted successfully.
@throws \Exception|\Throwable in case insert failed.
Inserts a row into the associated database table using the attribute values of this record .