yii2 Active Record Active records with sub queries


Example: Customers who can create a post. Each customer can create multiple posts. As soon as customer creates the post, post will be under administrators review. Now we have to fetch the customers list who have all active posts by using sub-query.

Note: If a customer has 5 post, among 5 posts if he has at least one inactive, we have to exclude this customer from the customers list.

$subQuery = Post::find()->select(['customer_id'])->where(['status' => 2]); //fetch the customers whos posts are inactive - subquery
$query = Customer::find()->where(['NOT IN', 'id', $subQuery])->all(); //Exclude the customers whos posts are inactive by using subquery