Codxplore.com

Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif

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 : 2599

Subscribe Us


Follow Us