Menus

Monday 14 August 2017

PHP more useful functions




PHP strlen() Function

The strlen() function returns the length of a string.

Syntax

strlen(string)

Example

<?php
echo strlen("Hello world!");
?>

PHP substr() Function


Return part of a string


The substr() function returns a part of a string.

Syntax

substr(string,start,length)



ParameterDescription
stringRequired. Specifies the string to return a part of
startRequired. Specifies where to start in the string
  • A positive number - Start at a specified position in the string
  • A negative number - Start at a specified position from the end of the string
  • 0 - Start at the first character in string
lengthOptional. Specifies the length of the returned string. Default is to the end of the string.
  • A positive number - The length to be returned from the start parameter
  • Negative number - The length to be returned from the end of the string

Get first n characters of a string

Examples

echo substr("Hello world",0,4);
Output is print  "Hello"


$category_name=substr($cat['description'],3,  strlen($cat['description'])-1);


PHP fmod() Function


The fmod() function returns the remainder (modulo) of x/y.

Syntax

fmod(x,y);

             if(fmod($i, 2)==0){                  

                  //  Reminder is zero.
             
            }
            else{

                //  Reminder is one.

            }



Converting separate month, day and year values into a timestamp



using setDate funtion

<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>




Tuesday 8 August 2017

YII2 Useful Tips


Filter to force lowercase on all operations


Yii change case each word in a string and save model.


Yii2 change model case

['student_name', 'filter', 'filter'=>'strtolower'],

['student_name', 'filter', 'filter'=>'strtoupper'],

To convert to Lowercase or Uppercase 





Thursday 3 August 2017

Yii2 Transaction


Yii2 save related records in single save call in single transaction


Database Transaction in Yii


A transaction is used to run a group of operations in single process. When you we run multiple query in single process, It will be used. If you get any problem, It will not complete successfully and also rollback the executed query.


Transaction represents a DB transaction.
It is usually created by calling yii\db\Connection::beginTransaction().
The following code is a typical example of using transactions (note that some DBMS may not support transactions):
$transaction = $connection->beginTransaction();
try {
    $connection->createCommand($sql1)->execute();
    $connection->createCommand($sql2)->execute();
    //.... other SQL executions
    $transaction->commit();
} catch (\Exception $e) {
    $transaction->rollBack();
    throw $e;
} catch (\Throwable $e) {
    $transaction->rollBack();
    throw $e;
}

Example

$transaction = Yii::$app->db->beginTransaction(); 

 try { 

 $user = new User(); 
 $user->name = 'Name'; 
 $user->save(); 

 $ua = new UserAddress(); 
 $ua->city = 'City'; 
 $user->link('userAddress', $ua); // <-- it creates new record in UserAddress table with ua.user_id = user.id 

 $transaction->commit(); 

 } catch (Exception $e) { 
 $transaction->rollBack(); 
 }


Example


if ($model->load(Yii::$app->request->post()))
        {
         
           $transaction = \Yii::$app->db->beginTransaction();
           try {  
         
         
            $stockitem=Stockitem::find()->where(['id'=>$model->stockitem_id])->one();
            $stockitem->stock=$stockitem->stock-$model->stock;
         
            $stockitem->save();
         
            //Stock add to center
            $st=  Stockcenter::find()->where(['stockitem_id'=>$model->stockitem_id,'center_ccid'=>$model->center_ccid])->one();
            if($st)
            {
              $st->stock=$st->stock+$model->stock;
              $st->amount=$model->amount;
              $st->save();
            }
            else
            {
                $st= new Stockcenter();
                $st->stockitem_id=$model->stockitem_id;
                $st->center_ccid=$model->center_ccid;
                $st->stock=$model->stock;
                $st->amount=$model->amount;
                $st->save();              
            }
         
         
            $model->date=date('Y-m-d',strtotime($model->date));
            $model->save();
         
            $transaction->commit();

            } catch (Exception $e) {
                        $transaction->rollBack();
            }
         
            //Redirect...
         
         
        }


Example

$transaction = Yii::$app->db->beginTransaction(); 
              
                try {
                    
                                     
                    $command=Yii::$app->db->createCommand('UPDATE receipt SET type="STUDENTS" , students_admission_number = '. $std->admission_number . ', students_ccid = '. $std->ccid .' WHERE type = "ENQUIRY" AND students_admission_number = '. $model->enquiry_number . ' and students_ccid = ' . $model->ccid);
                    
                    if($std->save() && $model->save() && $command->execute())                    
                        $transaction->commit();
                    
                } catch (\Exception $e) {
                    $transaction->rollBack();
                    throw $e;
                } catch (\Throwable $e) {
                    $transaction->rollBack();
                    throw $e;
                }


http://www.yiiframework.com/doc-2.0/yii-db-transaction.html