insert() public method
Inserts a document into the associated index using the attribute values of this record.
This method performs the following steps in order:
- call beforeValidate() when
$runValidation
is true. If validation fails, it will skip the rest of the steps; - call afterValidate() when
$runValidation
is true. - call beforeSave(). If the method returns false, it will skip the rest of the steps;
- insert the record into database. If this fails, it will skip the rest of the steps;
- 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 changed attribute values will be inserted into database.
If the 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 boolean insert ( $runValidation = true, $attributes = null, $options = ['op_type' => 'create'] ) | ||
---|---|---|
$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:
Please refer to the elasticsearch documentation for more details on these options. By default the |
return | boolean |
Whether the attributes are valid and the record is inserted successfully. |
Please login to continue.