Commit 3af9bb19 by Qiang Xue

Merge pull request #3776 from Ragazzo/sql_view_widgets_added

sql view mapping added , similar to CQRS query model
parents 27ef3f6a aa61a97b
...@@ -361,6 +361,75 @@ In `search()` you then just add another filter condition with `$query->andFilter ...@@ -361,6 +361,75 @@ In `search()` you then just add another filter condition with `$query->andFilter
> Info: For more information on `joinWith` and the queries performed in the background, check the > Info: For more information on `joinWith` and the queries performed in the background, check the
> [active record docs on eager and lazy loading](active-record.md#lazy-and-eager-loading). > [active record docs on eager and lazy loading](active-record.md#lazy-and-eager-loading).
#### Using sql views for filtering, sorting and displaying data
There is also other approach that can be faster and more useful - sql views. So for example if we need to show gridview
with users and their profiles we can do it in this way:
```php
CREATE OR REPLACE VIEW vw_user_info AS
SELECT user.*, user_profile.lastname, user_profile.firstname
FROM user, user_profile
WHERE user.id = user_profile.user_id
```
Then you need to create ActiveRecord that will be representing this view:
```php
namespace app\models\views\grid;
use yii\db\ActiveRecord;
class UserView extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'vw_user_info';
}
public static function primaryKey()
{
return ['id'];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
// define here your rules
];
}
/**
* @inheritdoc
*/
public static function attributeLabels()
{
return [
// define here your attribute labels
];
}
}
```
After that you can youse this UserView active record with search models, without additional specifying of sorting and filtering attributes.
All attributes will be working out of the box. Note that this approach has several pros and cons:
- you dont need to specify different sorting and filtering conditions and other things. Everything works out of the box;
- it can be much faster because of data size, count of sql queries performed (for each relation you will need additional query);
- since this is a just simple mupping UI on sql view it lacks of some domain logic that is in your entities, so if you will have some methods like `isActive`,
`isDeleted` or other that will influence on UI you will need to duplicate them in this class too.
### Multiple GridViews on one page ### Multiple GridViews on one page
You can use more than one GridView on a single page but some additional configuration is needed so that You can use more than one GridView on a single page but some additional configuration is needed so that
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment