Codxplore.com

Latest news:

Quran ul karim is a free Quran application for android. This application is advertisement free application for android smartphone. Download from https://play.google.com/store/apps/details?id=com.akramhossain.quranulkarim&hl=en

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')
                ->all();

Example of innerJoinWith():

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

Example of join():

$query = Posts::find();
$query->select(['id','title','description']) 
        ->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')
 ->all();

Example of Query Builder:

$query = new Query;
$query->select([
        'users.user_name',
        'categories.category_name',
        'documents.document_name'

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

Tags : PHP, Yii2,

Views : 1838

Subscribe Us


Follow Us