insert() public method
Inserts a row into the associated database table using the attribute values of this record.
This method performs the following steps in order:
- call beforeValidate() when
$runValidation
istrue
. If beforeValidate() returnsfalse
, the rest of the steps will be skipped; - call afterValidate() when
$runValidation
istrue
. If validation failed, the rest of the steps will be skipped; - call beforeSave(). If beforeSave() returns
false
, the rest of the steps will be skipped; - 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_AFTER_VALIDATE, EVENT_BEFORE_INSERT, and EVENT_AFTER_INSERT will be raised by the corresponding methods.
Only the 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:
$customer = new Customer; $customer->name = $name; $customer->email = $email; $customer->insert();
public boolean insert ( $runValidation = true, $attributes = null ) | ||
---|---|---|
$runValidation | boolean |
Whether to perform validation (calling validate()) before saving the record. Defaults to |
$attributes | array |
List of attributes that need to be saved. Defaults to |
return | boolean |
Whether the attributes are valid and the record is inserted successfully. |
throws | Exception |
in case insert failed. |
Please login to continue.