[Bug] Undefined variable

I have a file controller

and a function CreateAction:


$model->t_datetime  = new CDbException('NOW()');

            $model->t_fees = 5;

            

			if($model->save())

           $connection = Yii::app()->db;

                    $transaction=$connection->beginTransaction();

               try { 

	               	if(md5('pay')== $_GET['mod']){

Connection was declared (defined)

but when I submit then I have error:


PHP Error


Description


Undefined variable: connection

and IF I move: $connection = Yii::app()->db;


$model->t_datetime  = new CDbException('NOW()');

            $model->t_fees = 5;

             $connection = Yii::app()->db;

			if($model->save())

          

                    $transaction=$connection->beginTransaction();

               try { 

	               	if(md5('pay')== $_GET['mod']){

then I got Message:


Undefined variable: transaction


error in lint 115: 


00115:                     $transaction->rollBack();

How can i Solve it ?

Take a look at your brackets.

It seems that $model->save() fails and as a result the connection variable will not be initialized. You skipped the brackets which means that only the next line following the if statement will be enclosed. The line




$connection->beginTransaction();



will always be executed regardless if the save fails or not.

You are using CDbException, I think you are trying to use CDbExpression?

CDbException != CDbExpression :blink:

Thanks u, I Solved it

but now I still have problem with FindbyA…




$requestuid = Users::model()->findAllByAttributes(array('id'=>Yii::app()->user->id));

echo $requestuid->balance;

I want to get balance money, but when I execute then I got the message:


PHP Error

Description


Trying to get property of non-object


00072:             echo $requestuid->u_balance;

What can I do ?

FindAll always returns an array. If you want to retrieve a single object use "findByAttributes" instead of "findAllByAttributes"