[Yii][Firebird] Вызов хранимой процедуры при сохранении модели
Код:
public function insert($attributes=null)
{
if(!$this->getIsNewRecord())
throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.'));
if($this->beforeSave())
{
// $sql = 'select result from USER_modify (?,?,?);';
$sql = 'INSERT INTO users ( user_id,user_name, sort_order, is_active) VALUES (?,?,?,?)';
$sql_gen = 'SELECT gen_id(gen_users_id,1) from users';
$command = Yii::app()->db->createCommand($sql);
$gen = Yii::app()->db->createCommand($sql_gen);
$command->prepare();
$this->user_id = $gen->queryScalar();
$command->bindValue(1, $this->user_id);
$command->bindValue(2, $this->user_name);
$command->bindValue(3, 1);
$command->bindValue(4, 1);
$command->execute();
$this->_pk=$this->user_id;
$this->afterSave();
$this->setIsNewRecord(false);
$this->setScenario('update');
return true;
}
return false;
}
{
if(!$this->getIsNewRecord())
throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.'));
if($this->beforeSave())
{
// $sql = 'select result from USER_modify (?,?,?);';
$sql = 'INSERT INTO users ( user_id,user_name, sort_order, is_active) VALUES (?,?,?,?)';
$sql_gen = 'SELECT gen_id(gen_users_id,1) from users';
$command = Yii::app()->db->createCommand($sql);
$gen = Yii::app()->db->createCommand($sql_gen);
$command->prepare();
$this->user_id = $gen->queryScalar();
$command->bindValue(1, $this->user_id);
$command->bindValue(2, $this->user_name);
$command->bindValue(3, 1);
$command->bindValue(4, 1);
$command->execute();
$this->_pk=$this->user_id;
$this->afterSave();
$this->setIsNewRecord(false);
$this->setScenario('update');
return true;
}
return false;
}
Как решить?
Как это работает:
В контроллере:
Код:
public function actionJCreate()
{
$model=new User;
if(isset($_POST['User']))
{
//Если не установлено автоматическое завершение транзакций
$trans = new CDbTransaction(Yii::app()->db);
$model->attributes=$_POST['User'];
$model->sort_order = 1;
$model->is_active = 1;
$model->user_id = -1;
if($model->save()){
$trans->commit(); //Коммитим
$this->redirect('/users/user/');
}
}
{
$model=new User;
if(isset($_POST['User']))
{
//Если не установлено автоматическое завершение транзакций
$trans = new CDbTransaction(Yii::app()->db);
$model->attributes=$_POST['User'];
$model->sort_order = 1;
$model->is_active = 1;
$model->user_id = -1;
if($model->save()){
$trans->commit(); //Коммитим
$this->redirect('/users/user/');
}
}
Код:
public function insert($attributes=null)
{
if(!$this->getIsNewRecord())
throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.'));
if($this->beforeSave())
{
$sql = 'select result from user_modify (?,?,?);';
$command = Yii::app()->db->createCommand($sql);
$command->prepare();
$command->bindValue(1, $this->user_id);
$command->bindValue(2, $this->user_name);
$command->bindValue(3, 'new');
$row = $command->queryRow();
$command->reset();
$this->user_id = $row['result'];
$this->_pk=$this->user_id;
$this->afterSave();
$this->setIsNewRecord(false);
$this->setScenario('update');
return true;
}
return false;
}
{
if(!$this->getIsNewRecord())
throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.'));
if($this->beforeSave())
{
$sql = 'select result from user_modify (?,?,?);';
$command = Yii::app()->db->createCommand($sql);
$command->prepare();
$command->bindValue(1, $this->user_id);
$command->bindValue(2, $this->user_name);
$command->bindValue(3, 'new');
$row = $command->queryRow();
$command->reset();
$this->user_id = $row['result'];
$this->_pk=$this->user_id;
$this->afterSave();
$this->setIsNewRecord(false);
$this->setScenario('update');
return true;
}
return false;
}