yii\elasticsearch\ActiveRecord::insert PHP Méthode

insert() public méthode

This method performs the following steps in order: 1. call [[beforeValidate()]] when $runValidation is true. If validation fails, it will skip the rest of the steps; 2. call [[afterValidate()]] when $runValidation is true. 3. call [[beforeSave()]]. If the method returns false, it will skip the rest of the steps; 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_BEFORE_INSERT]], [[EVENT_AFTER_INSERT]] and [[EVENT_AFTER_VALIDATE]] will be raised by the corresponding methods. Only the [[dirtyAttributes|changed attribute values]] will be inserted into database. If the [[primaryKey|primary key]] is not set (null) during insertion, it will be populated with a randomly generated value after insertion. For example, to insert a customer record: ~~~ $customer = new Customer; $customer->name = $name; $customer->email = $email; $customer->insert(); ~~~
public insert ( boolean $runValidation = true, array $attributes = null, array $options = ['op_type' => 'create'] ) : boolean
$runValidation boolean whether to perform validation before saving the record. If the validation fails, the record will not be inserted into the database.
$attributes array list of attributes that need to be saved. Defaults to null, meaning all attributes will be saved.
$options array options given in this parameter are passed to elasticsearch as request URI parameters. These are among others: - `routing` define shard placement of this record. - `parent` by giving the primaryKey of another record this defines a parent-child relation - `timestamp` specifies the timestamp to store along with the document. Default is indexing time. Please refer to the [elasticsearch documentation](http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html) for more details on these options. By default the `op_type` is set to `create`.
Résultat boolean whether the attributes are valid and the record is inserted successfully.
    public function insert($runValidation = true, $attributes = null, $options = ['op_type' => 'create'])
    {
        if ($runValidation && !$this->validate($attributes)) {
            return false;
        }
        if (!$this->beforeSave(true)) {
            return false;
        }
        $values = $this->getDirtyAttributes($attributes);
        $response = static::getDb()->createCommand()->insert(static::index(), static::type(), $values, $this->getPrimaryKey(), $options);
        $pk = static::primaryKey()[0];
        $this->{$pk} = $response['_id'];
        if ($pk != '_id') {
            $values[$pk] = $response['_id'];
        }
        $this->_version = $response['_version'];
        $this->_score = null;
        $changedAttributes = array_fill_keys(array_keys($values), null);
        $this->setOldAttributes($values);
        $this->afterSave(true, $changedAttributes);
        return true;
    }