Latest news:

Quran ul karim is a free Quran application for android. This application is advertisement free application for android smartphone. Download from

Yii2 joinWith(), innerJoinWith(), join(), leftJoin()

Yii2 joinWith(), innerJoinWith(), join(), leftJoin()

Yii2 active record has powerful built-in feature to join database table. 
In this post we will learn some of the useful features

  • joinWith()
  • innerJoinWith(),
  • join()
  • leftJoin()
  • join using query builder

Example of joinWith():

$model = Users::find()
                ->joinWith('posts', false, 'INNER JOIN')

Example of innerJoinWith():

$model = Users::find()
                ->innerJoinWith('posts', false)

Example of join():

$query = Posts::find();
        ->join('LEFT JOIN','users','users.user_id =posts.user_id');
$data = $query->all();

Example of leftJoin():

$query = Posts::find()
 ->leftJoin('users', 'users.user_id = posts.user_id)
 ->leftJoin('meta_data', 'meta_data.meta_id = posts.meta_data_id')

Example of Query Builder:

$query = new Query;

    ->join('LEFT OUTER JOIN', 'categories', 'categories.user_id = users.user_id')        
    ->join('LEFT OUTER JOIN', 'documents', 'category.category_id = documents.document_id')
$command = $query->createCommand();
$data = $command->queryAll(); 

Tags : PHP, Yii2,

Views : 1838

Subscribe Us

Follow Us