You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to search data from 2 table (with same field: email, phone, ...)
here is my code in search model:
// .... some code before$columns = ['id', 'name', 'status']; // columns example in 2 tables$query = newQuery;
$query->andFilterWhere( [ 'email' => $this->email, 'phone' => $this->phone ] )
->andFilterWhere(.......); // .... and more conditional$q = clone $query; // I don't want to rewrite **where** conditional$query->select(ArrayHelper::merge($columns, ['"tableAAAAA" as `table`']))
->from(ModelAAAAA::tableName());
$q->select(ArrayHelper::merge($columns, ['"tableBBBBB" as `table`']))
->from(ModelBBBBB::tableName());
$query->union($q, true); // I need all records$dataProvider = newActiveDataProvider([
'query' => $query,
]);
return$dataProvider; // for GridView
By default, Yii will limit 20 records, when I see in debug, SQL will look like:
(SELECT`id`, `name`, `status`, "tableAAAAA"as`table`FROM`tbl_tableAAAAA`WHERE (`email`='abcd@gmail.com') AND (`phone`='0123')
LIMIT20)
UNION ALL
(SELECT`id`, `name`, `status`, "tableBBBBB"as`table`FROM`tbl_tableAAAAA`WHERE (`email`='abcd@gmail.com') AND (`phone`='0123')
)
=> Yii will get all records from tableBBBBBB
but I want SQL look like:
(SELECT`id`, `name`, `status`, "tableAAAAA"as`table`FROM`tbl_tableAAAAA`WHERE (`email`='abcd@gmail.com') AND (`phone`='0123')
)
UNION ALL
(SELECT`id`, `name`, `status`, "tableBBBBB"as`table`FROM`tbl_tableAAAAA`WHERE (`email`='abcd@gmail.com') AND (`phone`='0123')
)
LIMIT20
Currently, my solution is:
$q->limit = &$query->limit;
so SQL will be look like:
(SELECT`id`, `name`, `status`, "tableAAAAA"as`table`FROM`tbl_tableAAAAA`WHERE (`email`='abcd@gmail.com') AND (`phone`='0123')
LIMIT20)
UNION ALL
(SELECT`id`, `name`, `status`, "tableBBBBB"as`table`FROM`tbl_tableAAAAA`WHERE (`email`='abcd@gmail.com') AND (`phone`='0123')
LIMIT20)
Do you have better idea?
Thanks,
The text was updated successfully, but these errors were encountered:
I want to search data from 2 table (with same field: email, phone, ...)
here is my code in search model:
By default, Yii will limit 20 records, when I see in debug, SQL will look like:
=> Yii will get all records from tableBBBBBB
but I want SQL look like:
Currently, my solution is:
so SQL will be look like:
Do you have better idea?
Thanks,
The text was updated successfully, but these errors were encountered: