Skip to content

Instantly share code, notes, and snippets.

@cyphercodes
Last active September 28, 2015 23:06
Show Gist options
  • Save cyphercodes/983074114ac8935736f7 to your computer and use it in GitHub Desktop.
Save cyphercodes/983074114ac8935736f7 to your computer and use it in GitHub Desktop.
CODE:
Branch::find()
->where(['id' => $id])
->with(['branchcategories' => function($q) {
$q->select(['id', 'name', 'branch_id', '(select COUNT(*) from branchitem where branchcategory_id=branchcategory.id AND status = ' . Branchitem::STATUS_ACTIVE . ') AS thecount'])->where(['status' => Branchcategory::STATUS_ACTIVE])->having('thecount > 0');
}])->with(['branchcategories.branchitems' => function($q) {
$q->select(['id', 'name', 'description', 'price', 'branchcategory_id'])->where(['status' => Branchitem::STATUS_ACTIVE]);
}])
->with(['branchcategories.branchitems.branchitempictures' => function($q) {
$q->select(['id', 'src_thumb', 'restoitem_id'])->orderBy("id desc")->limit(1);
}])
->select(['id'])
->asArray()
->one();
-------------------------------------------
EXECUTED QUERIES FROM DEBUGGER:
SELECT SELECT `id`, `src_thumb`, `restoitem_id` FROM `branchitempicture` WHERE `restoitem_id` IN ('37', '42', '29', '36', '39', '41', '30', '40', '28', '38') ORDER BY `id` DESC LIMIT 1
SELECT SELECT `id`, `name`, `description`, `price`, `branchcategory_id` FROM `branchitem` WHERE (`status`=10) AND (`branchcategory_id` IN ('4', '10')) ORDER BY `name`
SELECT SELECT `id` FROM `branch` WHERE `id`=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment