My action create:
public function actionCreate() {
$model = new User('create');
if (isset($_POST['User'])) {
$model->attributes = $_POST['User'];
$transaction = $model->dbConnection->beginTransaction();...
why the attributes are null and i get this error on beginTransaction
Fatal error: Call to a member function beginTransaction() on a non-object
If i change :
$model = new User('create');
to
$model = new User;
i dont get the error.
I had to specify the (‘create’) for the rule:
array('pass','required','on'=>'create'),
to work
This is the way you should do your transactions:
if(isset($_POST['User']))
{
$connection=Yii::app()->getDb();
$transaction=$connection->beginTransaction();
$model=new User('create');
try
{
$model->attributes=$_POST['user'];
if(!$model->save())
throw new Exception(CHtml::errorSummary($model));
$transaction->commit();
}
catch(Exception $e)
{
$transaction->rollBack();
$error=$e->getMessage();
}
}
zipzapduo
(Zipzapduo)
July 8, 2012, 10:24pm
4
Thank you very much.One thing.what does this code?:
$error=$e->getMessage();
Take a look here http://php.net/manual/en/language.exceptions.php
Basically it catches the error thrown in the try{} portion of the code[throw new Exception(CHtml::errorSummary($model));]
zipzapduo
(Zipzapduo)
July 8, 2012, 11:05pm
6
yes i understand the catch of the exception. What i dont understand is where the $error variable is shown
Oh come on now
after the try/catch block you can check if(!empty($error)) and use the $error variable in the view to show the error to the user.
Smth along these lines:
//controller
public function actionSomething(){
$error=null;
if(isset($_POST['User']))
{
$connection=Yii::app()->getDb();
$transaction=$connection->beginTransaction();
$model=new User('create');
try
{
$model->attributes=$_POST['user'];
if(!$model->save())
throw new Exception(CHtml::errorSummary($model));
$transaction->commit();
}
catch(Exception $e)
{
$transaction->rollBack();
$error=$e->getMessage();
}
}
$this->render('your-view',array('error'=>$error));
}
//view
<?php if(!empty($error)): echo $error; endif; ?>
Now i have this error on catch when validates:
Property "UserController.pass" is not defined.
public function actionCreate() {
$model = new User('create');
if (isset($_POST['User'])) {
$connection = Yii::app()->getDb();
$transaction = $connection->beginTransaction();
try {
$model->attributes = $_POST['User'];
$palavra = $model->pass;
if ($model->validate()) {...<
You’re doing $this->pass instead of $model->pass somewhere in your controller code, that’s the problem.
zipzapduo
(Zipzapduo)
July 9, 2012, 8:00pm
11
yes that’s what i thought too but i cant find,here’s my entire action create:
public function actionCreate() {
Yii::import('ext.yii-mail.YiiMailMessage');
$model=new User;
if (isset($_POST['User'])) {
$connection = Yii::app()->getDb();
$transaction = $connection->beginTransaction();
$model = new User('create');
try {
$model->attributes = $_POST['User'];
$palavra = $model->pass;
if ($model->validate()) {
$model->save();
$message = new YiiMailMessage;
$message->setBody('<h1>StyleStore</h1>Efectuou o registo com sucesso no style store,já pode fazer login.<br><br>
Utilizador: ' . $model->username . '<br>Password: ' . $palavra . '<br><br>Obrigado pelo seu registo.<br><br>Cumprimentos.', 'text/html');
$message->setTo($model->email);
$message->subject = 'Registo de Utilizador';
$message->from = Yii::app()->params['adminEmail'];
Yii::app()->mail->send($message);
$message2 = new YiiMailMessage;
$message2->subject = 'Registo de Utilizador';
$message2->setBody('<h1>StyleStore</h1>Foi registado um novo utilizador com o nome ' . $model->nome, 'text/html');
$message2->setTo(Yii::app()->params['adminEmail']);
$message2->from = Yii::app()->params['adminEmail'];
Yii::app()->mail->send($message2);
Yii::app()->user->setFlash('success', Yii::t('app', 'Data saved! An email was sent to confirm the credentials'));
$transaction->commit();
if (Yii::app()->user->checkAccess('Admin'))
$this->redirect(array('view', 'id' => $model->iduser));
else
$this->redirect(Yii::app()->homeUrl);
}
} catch (Exception $e) {
$transaction->rollBack();
$error = $e->getMessage();
Yii::app()->user->setFlash('error', Yii::t('app', $e->message));
$this->redirect('create');
}
}
$this->render('create', array(
'model' => $model,
));
}
what about the view ? have you checked there ?
Anyway, the yii debugger should highlight the error for you, so track the stack of messages and see where it dies.
zipzapduo
(Zipzapduo)
July 10, 2012, 6:16pm
13
Strange .I just get this if i use the netbeans-xdebug.If i run without debugging all gets fine.