Category Archives: Yii Framework 2.0

Installing Yii2.0

We can install Yii in two ways, using Composer or downloading an archive file. The former is the preferred way as it allows you to install new extensions or update Yii by running a single command. Install Composer Installing Yii Via Composer Installing from an Archive File Getting Started Install Composer Download the composer from getcomposer.org […]

Yii Framework 2 : Menu Widget

Yiiframewokr 2.0 ‘yii\widgets\Menu’ widgets is used to display the multi-level menu items using nested HTML lists. ‘items’ is the main property of Menu. It contains all the menu items with attributes. A menu can contain the sub-menu and their properties like class, style etc. So default Yii 2.0 menu widget is giving a flexible way […]

Yii Framework 2 : Removing index.php from URL

To hide the ‘index.php’ and enable the Pretty URL in yiiframework 2.0, this post will help you. For this we have to configure the .htaccess and web.php file. .htaccess Please add the following lines in ‘.htaccess’ file inside the ‘web’ directory of yii2.0 application. RewriteEngine on # If a directory or a file exists, use […]

Yii Framework 2 : Advanced Select Query

Scopes() Scopes Sample 1: class User extends \yii\db\ActiveRecord { namespace app\models; public static function olderThan($query, $age = 5) { $query->andWhere(‘userid > :age’, [‘:age’ => $age]); } } // call the scope function $model = User::find()->olderThan(5)->all(); Sample 2: class User extends \yii\db\ActiveRecord { // … public static function active($query) { $query->andWhere(‘status = 1’); } } // […]

Yii Framework 2 : Joins Query

Relation Model joinWith() innerJoinWith() Join() leftJoin() Relation Model $model = User::find() ->with(‘comments’) ->all(); foreach ($model as $user) { // get data from relation model $comments = $user->comments; …… foreach($comments as $comment){ …….. } } joinWith() Sample 1: $model = User::find() ->joinWith(‘comments’) ->all(); Sample 2: $model = User::find() ->joinWith(‘comments’) ->orderBy(‘tbl_comments_id.id, tbl_user.id’) ->all(); innerJoinWith() $model = User::find() […]

Yii Framework 2 : Select Query For Model

This tutorial will help you to get the list of function to retrieve or select the data from the database using model of yii2.0 framework. I updated most of the functions it will helpful for yii2.0 select query method. find() select() all() one() where() orderBy() count() asArray() indexBy() limit() offset() Limit With Pagination() LIKE Condition […]

Yii Framework 2 : SQL Select Query

findBySql() queryAll() queryOne() queryColumn() queryScalar Query With Prepared Statements() Query Class Note //Before use the Query Class, We have to add the following namespace use yii\db\Query; $connection = \Yii::$app->db; findBySql Sample 1: $sql = ‘SELECT * FROM tbl_user’; $model = User::findBySql($sql)->all(); Sample 2: $sql = ‘SELECT * FROM tbl_user’; $model = User::findBySql($sql)->one(); queryAll $model = $connection->createCommand(‘SELECT […]

Yii Framework 2 : SqlDataProvider

SqlDataProvider Formats SqlDataProvider To Gridview SqlDataProvider To Excel Yii2.0 SqlDataProvider is used to get the data provider using plain SQL statement. SqlDataProvider is return the data provider as array for each row of query result. We can use the sorting, pagination in SqlDataProvider using $sort and $pagination parameter. Pagination: If we want pagination concept, We […]

Yii Framework 2 : Transaction

Transaction: 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. beginTransaction() Note //Before use the Query Class, We have to add the following […]