Creates new records in the database.
public create ( Model $Model, array $fields = null, array $values = null ) : boolean | ||
$Model | Model | Model object that the record is for. |
$fields | array | An array of field names to insert. If null, $Model->data will be used to generate field names. |
$values | array | An array of values with keys matching the fields. If null, $Model->data will be used to generate values. |
return | boolean | Success |
/** * Generates and executes an SQL INSERT statement for given model, fields, and values. * Removes Identity (primary key) column from update data before returning to parent, if * value is empty. * * @param Model $model * @param array $fields * @param array $values * @param mixed $conditions * @return array */ function create(&$model, $fields = null, $values = null) { if (!empty($values)) { $fields = array_combine($fields, $values); } $primaryKey = $this->_getPrimaryKey($model); if (array_key_exists($primaryKey, $fields)) { if (empty($fields[$primaryKey])) { unset($fields[$primaryKey]); } else { $this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($model) . ' ON'); } } $result = parent::create($model, array_keys($fields), array_values($fields)); if (array_key_exists($primaryKey, $fields) && !empty($fields[$primaryKey])) { $this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($model) . ' OFF'); } return $result; }
/** * Generates and executes an SQL INSERT statement for given model, fields, and values. * Removes Identity (primary key) column from update data before returning to parent, if * value is empty. * * @param Model $model * @param array $fields * @param array $values * @return array */ public function create(Model $model, $fields = null, $values = null) { if (!empty($values)) { $fields = array_combine($fields, $values); } $primaryKey = $this->_getPrimaryKey($model); if (array_key_exists($primaryKey, $fields)) { if (empty($fields[$primaryKey])) { unset($fields[$primaryKey]); } } $result = parent::create($model, array_keys($fields), array_values($fields)); return $result; }