Tag Archives: yii framework development

Yii session


Yii session : Session is used to store data which persists across the multiple pages. Yii provides session management in object oriented style. Here in this tutorial we are going to explain how you can create session and get it across the request.


Yii session

Yii set session variable

You can set session variable in yii as below –

Yii session: Set Session Variable Example

$app = app();
$session = Yii::$app->session;
$session->set('userId', 124322);
//or
$session['userId'] = 124322;

In the above example we have created a session variable userId which stores the value 124322. Session variable can be set either using the $session[‘userId’] variable or $session->set(‘userId’, 124322); both will set the session variable with the given value.

Yii get session variable

You can get session variable in yii as below –

Yii session: Get Session Variable Example

$app = app();
$session = Yii::$app->session;
$userId = $session->get('userId');
//or
$userId  = $session['userId'];
echo $userId; // it will give the 124322

If you want to access the session variable value in yii you can use the syntax as above which will give you the session value.

Yii clear session : Remove | Unset Session variable

You can clear session variable in yii as below –

Yii clear session: destroy session Example

$app = app();
$session = Yii::$app->session;
$session->remove('userId');

If you want to unset the session variable value in yii you can use the syntax as above which will remove the session variable.


More About Yii Session

Let Us have some more example on yii session-


Yii Session Flash Data

Flash data is a special kind of session variable which is set for one request and destroyed for other request automatically.

Yii Session Set Flash Data: Example

$app = app();
$session = Yii::$app->session;
$session->setFlash('successMessgae', 'Data updated successfully.');

This becomes important when you are dealing with the messages which needs to be displayed only once example – You want to show the success message when data is updated and destroy the message after this. The above example contains the same example.

Yii Session Get Flash Data: Example

Yii Session Get Flash Data: Example

$app = app();
$session = Yii::$app->session;
echo $session->getFlash('successMessgae'); // will print - "Data updated successfully."

The above example will show the flash message stored in the variable successMessage. If you print the above example it will give you – Data updated successfully.

Yii Get Last Inserted Id


Yii Get Last Inserted Id: If you are working with Yii models(database tables) and performing insert operation. You can get the last inserted id as below.


Syntax for Yii Get Last Inserted Id

Below is example of get Last inserted id in Yii.

Yii Get Last Inserted Id Using Models

If You are using models you can get Last inserted id as –

if($model->save())
{
    $LastInsertedId = $model->id 
   // this will be the last inserted id
}

if You Are Using Yii Pdo


$lastInsertedId = Yii::app()->db->getLastInsertId();

Which will return the last inserted id in the Yii table.

Yii Get Current Date Time


Yii Get Current date time: You can use Yii::app()->Date->now(); to get current date time in Yii.


Syntax for Yii Get Current Date Time


$date = Yii::app()->Date->now();
echo $date;

The above method will give the current date time in Yii.

Yii Delete Query Builder


Yii Delete Query Builder : You can delete the records using query builder as below.


Syntax : Yii Delete Query Builder


$command = Yii::app()->db->createCommand()
    ->delete('users', 'id =' . $id);

This will generate the following query.

DELETE FROM users where id = “$id”;

Yii Redirect to url


Yii Redirect to url : You can redirect to the url using the $this->rediect() method.


Syntax : Yii Redirect to url

First create url and then redirect to the location.


$url = Yii::app()->createUrl('site/Home');
$this->redirect($url);

The above method will redirect to the given url.

Yii Get Current Url


Yii Get Current Url : Current url in yii is obtained by using the method Url::current();


Syntax : Yii Get Current Url


$currentUrl = Url::current();

The above method returns the current url with query string in yii.

Yii Get Base Url


Yii Get Base Url : You can get Base url using the Yii::app()->baseUrl method


Syntax : Yii Get Base Url


$baseUrl = Yii::app()->baseUrl;

echo $baseUrl;

You can use the above method to get base url.

Yii update query builder


Yii update query builder : Yii::app()->db->createCommand()->update(tableName,data,condition) is used to update data in yii.


Yii update query builder Syntax with Example

You can update table data as below :

$data = array(
"name" => "John",
"email" => "john@example.com"
);
$update = Yii::app()->db->createCommand()
    ->update('users', 
        $data,
        'id=:id',
        array(':id'=>$id)
    );

Where $id is primary key in user table and $data is associative array containing the column name and values.
Which will produce the following output :

UPDATE users set name = “John” AND email = “john@example.com” where id = “$id”

Yii Insert data query builder


Yii Insert data : Yii Query builder is used to insert data in yii. Yii::app()->db->createCommand()->insert($tableName, $dataArray) is used to insert data. It accepts two parameters first table name and second the array data which needs to be inserted.


Syntax : Yii Insert data query builder

Below is yii insert query example.

Yii::app()->db->createCommand()->insert($tableName,$dataArray );

Where $tableName is table name in which you want to insert data. $dataArray is associate array containing the data and column name.

Yii Insert data query builder example

$tableName = "Users";
$dataArray = array(
"name" => "Kelly",
"email" => "keylly@eample.com",
"country" => "Spain"
);
Yii::app()->db->createCommand()->insert($tableName,$dataArray );

The above example will produce the following output.

insert into users (name,email,country) values(“Kelly”,”kelly@example.com,”Spain”)