本文整理汇总了PHP中CActiveRecord::save方法的典型用法代码示例。如果您正苦于以下问题:PHP CActiveRecord::save方法的具体用法?PHP CActiveRecord::save怎么用?PHP CActiveRecord::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CActiveRecord
的用法示例。
在下文中一共展示了CActiveRecord::save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: update
/**
* @param CActiveRecord $model
* @param $sortableAttribute
* @param $sortOrderData
*
* @return string
*/
private function update($model, $sortableAttribute, $sortOrderData)
{
$pk = $model->tableSchema->primaryKey;
$pk_array = array();
if (is_array($pk)) {
// composite key
$string_ids = array_keys($sortOrderData);
$array_ids = array();
foreach ($string_ids as $string_id) {
$array_ids[] = explode(',', $string_id);
}
foreach ($array_ids as $array_id) {
$pk_array[] = array_combine($pk, $array_id);
}
} else {
// normal key
$pk_array = array_keys($sortOrderData);
}
$models = $model->model()->findAllByPk($pk_array);
$transaction = Yii::app()->db->beginTransaction();
try {
foreach ($models as $model) {
$_key = is_array($pk) ? implode(',', array_values($model->primaryKey)) : $model->primaryKey;
$model->{$sortableAttribute} = $sortOrderData[$_key];
$model->save();
}
$transaction->commit();
} catch (Exception $e) {
// an exception is raised if a query fails
$transaction->rollback();
}
}
示例2: checkUpdatedAtField
/**
* @param CActiveRecord $record
* @param string $colName
* @param string $newValue
*/
protected function checkUpdatedAtField(CActiveRecord $record, $colName = 'name', $newValue = 'xxxxx')
{
$oldColumnValue = $record->{$colName};
// Modifier l'objet met le champ updated_at à jour
$oldUpdatedAt = $record->updated_at;
$record->{$colName} = $newValue;
$this->assertTrue($record->save());
$this->assertNotEquals($oldUpdatedAt, $record->updated_at);
// restauration de la valeur de départ
$record->{$colName} = $oldColumnValue;
$this->assertTrue($record->save());
}
示例3: Save
/**
* Переопределенная функция сохранения группы LDAP.
* Помимо сохранения данных группы LDAP, присваиваются выбранные права доступа
*
* @param type $runValidation
* @param type $attributes
* @throws CHttpException
*/
public function Save($runValidation = true, $attributes = null)
{
/* $selectedRows - массив выбранных ролей группы LDAP */
$selectedRows = (string) filter_input(INPUT_POST, 'items') !== '' ? json_decode((string) filter_input(INPUT_POST, 'items'), true) : [];
/* $_POST['oper'] может быть "edit" или "add", редактирование или добавление новой записи */
if ((string) filter_input(INPUT_POST, 'oper') === '') {
throw new CHttpException(500, 'Отсутствует POST переменная "oper"');
}
/* ИД группы LDAP, который редактируется, если необходим */
$editid = (string) filter_input(INPUT_POST, 'editid');
$oper = (string) filter_input(INPUT_POST, 'oper');
if ($oper === 'edit' && $editid === '') {
throw new CHttpException(500, 'Отсутствует POST переменная "editid"');
}
/* Сохраняем модель группы LDAP и присваиваем роли */
if (parent::save($runValidation, $attributes) !== false) {
/* $auth = Yii::app()->authManager; */
/* Удаляем все роли группы LDAP при сохранении изменений профиля групп LDAP */
if ($editid !== '') {
AuthAssignmentLdap::model()->deleteAll('groupid = :groupid', array(':groupid' => $editid));
}
/* Присваиваем выбранные роли группе LDAP */
if ($editid === '' && $oper === 'add') {
$editid = parent::getPrimaryKey();
}
if (count((array) $selectedRows) > 0) {
foreach (array_keys($selectedRows) as $AuthItem) {
$model = new AuthAssignmentLdap();
$model->itemname = $AuthItem;
$model->groupid = $editid;
$model->save();
}
}
}
}
示例4: assertSaves
/**
* Assert thet the model can be saved without error and, if errors are present, print
* out the corresponding error messages.
* @param CActiveRecord $model
*/
public function assertSaves(CActiveRecord $model)
{
$saved = $model->save();
if ($model->hasErrors()) {
VERBOSE_MODE && print_r($model->getErrors());
}
$this->assertTrue($saved);
}
示例5: save
public function save($runValidation = true, $attributes = null)
{
if (in_array($this->tableName(), array('{{post}}', '{{onlinehelp}}'))) {
Yii::app()->user->setFlash('warning', '<strong>' . Yii::t('site', 'Info') . '</strong><br />' . Yii::t('site', 'Not allowed to save items in table {tableName} in this demo version.', array('{tableName}' => str_replace(array('{{', '}}'), array('', ''), $this->tableName()))));
return true;
}
return parent::save($runValidation, $attributes);
}
示例6: saveActiveRecord
public function saveActiveRecord(CActiveRecord $record)
{
try {
if (!$record->save()) {
new \Error(5, null, json_encode($record->getErrors()));
}
} catch (Exception $e) {
new \Error(5, null, $e->getMesaage());
}
}
示例7: save
public function save($runValidation = true, $attributes = null)
{
parent::save($runValidation, $attributes);
if ($this->rank == null) {
$this->rank = $this->id;
$this->setIsNewRecord(false);
$this->save(false);
}
return true;
}
示例8: saveFromPost
/**
* Attempts to save the specified model with attribute values from POST. If
* the saving fails or if no POST data is available it returns false.
* @param CActiveRecord $model the model
* @return boolean whether the model was saved
*/
protected function saveFromPost(&$model)
{
if (isset($_POST[$this->getModelClass()])) {
$model->attributes = $_POST[$this->getModelClass()];
if ($model->save()) {
return true;
}
}
return false;
}
示例9: save
public function save($runValidation = true, $attributes = NULL)
{
$status = parent::save($runValidation, $attributes);
if (!$this->getIsInversed()) {
$inversed = new Contact();
$inversed->user_id = $this->contact_id;
$inversed->contact_id = $this->user_id;
$inversed->save(false);
}
return $status;
}
示例10: save
public function save($runValidation = true, $attributes = NULL)
{
$a = parent::save($runValidation, $attributes);
if ($this->public && $this->hash == '') {
$download = new Download();
$download->file_id = $this->id;
$download->company_id = Yii::app()->user->Company;
$this->hash = $download->id = md5(mt_rand());
$download->save();
}
return $a;
}
示例11: save
public function save($runValidation = true, $attributes = null)
{
// Save default store
if ($this->store_id === 0) {
Yii::app()->settings->setValue('config_title', $this->name);
//TODO: Add additional fields here
return true;
} else {
parent::save($runValidation, $attributes);
//TODO: Add additional config fields here
}
}
示例12: save
public function save($runValidation = true, $attributes = null)
{
$user = Yii::app()->user;
if ($user instanceof CWebUser) {
$model = self::model()->find(['scopes' => ['active'], 'condition' => 'user_id=:userId AND token=:token AND platform=:platform', 'params' => [':userId' => $user->id, ':token' => $this->token, ':platform' => $this->platform]]);
if ($model instanceof PushDevice) {
$this->setIsNewRecord(false);
$this->setPrimaryKey($model->id);
}
}
return parent::save($runValidation, $attributes);
}
示例13: update
/**
*### .update()
*
* main function called to update column in database
*
*/
public function update()
{
//get params from request
$this->primaryKey = yii::app()->request->getParam('pk');
$this->attribute = yii::app()->request->getParam('name');
$this->value = yii::app()->request->getParam('value');
//checking params
if (empty($this->attribute)) {
throw new CException(Yii::t('TbEditableSaver.editable', 'Property "attribute" should be defined.'));
}
if (empty($this->primaryKey)) {
throw new CException(Yii::t('TbEditableSaver.editable', 'Property "primaryKey" should be defined.'));
}
//loading model
$this->model = CActiveRecord::model($this->modelClass)->findByPk($this->primaryKey);
if (!$this->model) {
throw new CException(Yii::t('TbEditableSaver.editable', 'Model {class} not found by primary key "{pk}"', array('{class}' => get_class($this->model), '{pk}' => is_array($this->primaryKey) ? CJSON::encode($this->primaryKey) : $this->primaryKey)));
}
//set scenario
$this->model->setScenario($this->scenario);
//commented to be able to work with virtual attributes
//see https://github.com/vitalets/yii-bootstrap-editable/issues/15
/*
//is attribute exists
if (!$this->model->hasAttribute($this->attribute)) {
throw new CException(Yii::t('EditableSaver.editable', 'Model {class} does not have attribute "{attr}"', array(
'{class}'=>get_class($this->model), '{attr}'=>$this->attribute)));
}
*/
//is attribute safe
if (!$this->model->isAttributeSafe($this->attribute)) {
throw new CException(Yii::t('editable', 'Model {class} rules do not allow to update attribute "{attr}"', array('{class}' => get_class($this->model), '{attr}' => $this->attribute)));
}
//setting new value
$this->setAttribute($this->attribute, $this->value);
//validate attribute
$this->model->validate(array($this->attribute));
$this->checkErrors();
//trigger beforeUpdate event
$this->beforeUpdate();
$this->checkErrors();
//saving (no validation, only changed attributes)
if ($this->model->save(false, $this->changedAttributes)) {
//trigger afterUpdate event
$this->afterUpdate();
} else {
$this->error(Yii::t('TbEditableSaver.editable', 'Error while saving record!'));
}
}
示例14: save
public function save($runValidation = TRUE, $attributes = NULL)
{
if ($this->is_active === NULL || $this->is_active == 0) {
$this->is_active = $this::ACCOUNT_TEAM_MEMBER_IS_DEACTIVATED;
}
// check permission
// if (!$this->accepting_invitation)
// {
// if ($this->isNewRecord && !Permission::checkPermission($this, PERMISSION_ACCOUNT_TEAM_MEMBER_CREATE))
// return false;
// if (!$this->isNewRecord && !Permission::checkPermission($this, PERMISSION_ACCOUNT_TEAM_MEMBER_UPDATE))
// return false;
// }
return parent::save($runValidation, $attributes);
}
示例15: save
public function save($runValidation = true, $attributes = NULL)
{
$users = User::model()->findAll();
parent::save($runValidation, $attributes);
foreach ($users as $user) {
if (!UserIncomeMap::model()->findByPk(array('user_id' => $user->id, 'itemVatCat_id' => $this->id))) {
//'user_id', 'itemVatCat_id'
$model = new UserIncomeMap();
$attr = array("user_id" => $user->id, "itemVatCat_id" => $this->id, "account_id" => 100);
$model->attributes = $attr;
if (!$model->save()) {
return false;
}
}
}
}