{"id":335810,"date":"2022-07-17T21:00:09","date_gmt":"2022-07-17T21:00:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=335810"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=335810","title":{"rendered":"<span>\u0412\u0438\u0434\u0436\u0435\u0442\u044b \u0434\u0430\u043d\u043d\u044b\u0445 Yii2 \u0438 DTO<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0411\u0430\u0437\u043e\u0432\u043e Yii2 \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u043d\u0430\u043c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0443 MVC (\u043c\u043e\u0434\u0435\u043b\u044c, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440). \u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0435\u043c \u043a \u0447\u0438\u0441\u0442\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 (\u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/habr.com\/ru\/post\/269589\/\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e<\/a> \u0434\u043b\u044f \u043e\u0431\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f) \u0438 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043d\u0435\u0451 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0442 Active Record \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u0445 (\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u0445), \u0442.\u043a. AR \u044d\u0442\u043e \u0447\u0430\u0441\u0442\u044c \u0441\u043b\u043e\u044f \u043f\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u0440\u0443\u0433\u0438\u043c \u0441\u043b\u043e\u044f\u043c \u0437\u043d\u0430\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0432\u0438\u0434\u0436\u0435\u0442\u044b \u043f\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u0445: <code>DeatilView<\/code>, <code>ListView<\/code> \u0438 <code>GridView<\/code>. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0434\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 <code>ActiveDataProvider<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0441\u0435\u0431\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 Active Record \u043c\u043e\u0434\u0435\u043b\u0438 &#8212; \u0446\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0438\u0445 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e DTO.<\/p>\n<h2>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h2>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0432 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u043a\u043e\u0434\u0443.<\/p>\n<p>\u041f\u0443\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<ol>\n<li>\n<p>\u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0438\u0445 (\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u0430\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f) \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u0432 \u0441\u0435\u0440\u0432\u0438\u0441<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043d\u0435\u043a\u0443\u044e \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0411\u0414, \u043e\u043d \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044e<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 SQL \u0437\u0430\u043f\u0440\u043e\u0441, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0443\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0435\u0442 \u0432 DTO \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0435\u0440\u0432\u0438\u0441\u0443<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u0441\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0443<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f View Render (\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f) \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 HTML \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u043f\u0443\u043d\u043a\u0442\u0435 6, \u043c\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 <code>ActiveDataProvider<\/code> (\u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043b\u044e\u0431\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 <code>DataProviderInterface<\/code>), \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0445\u0440\u0430\u043d\u044f\u0449\u0438\u0435\u0441\u044f \u0432 \u043d\u0451\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u044d\u0442\u043e \u043d\u0435 Active Record \u043c\u043e\u0434\u0435\u043b\u0438, \u0430 Data Transfer Object (DTO).<\/p>\n<p>\u0421\u043b\u043e\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<ol>\n<li>\n<p>User Interface: \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 (\u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0438\u0445, \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0432 \u0441\u0435\u0440\u0432\u0438\u0441, \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f)<\/p>\n<\/li>\n<li>\n<p>Business Logic: \u0441\u0435\u0440\u0432\u0438\u0441\u044b \u0441 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u043e\u0439<\/p>\n<\/li>\n<li>\n<p>Data Access: \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 (\u0447\u0430\u0441\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0442\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043f\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 Active Record)<\/p>\n<\/li>\n<\/ol>\n<h2>\u041a\u043e\u0434<\/h2>\n<h3>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430<\/h3>\n<p><em>\u0414\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f, \u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0430 \u043d\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u0435\u0436\u0434\u0443 \u043a\u0430\u0436\u0434\u044b\u043c \u0441\u043b\u043e\u044f\u043c, \u044d\u0442\u043e \u0441\u0432\u043e\u0439 DTO \u0438 \u043f\u0440.).<\/em><\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 &#171;\u0431\u043b\u043e\u0433&#187; \u0441\u043e \u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438.<\/p>\n<p>\u0424\u0430\u0439\u043b\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430:<\/p>\n<pre><code>app - - controllers - - - - ArticleController.php - - views - - - - article - - - - - - grid.php - - - - - - list.php - - - - - - list_item.php - - - - - - detail.php - - services - - - - article - - - - - - builders - - - - - - - - ArticleDtoBuilder.php - - - - - - dtos - - - - - - - - ArticleDto.php - - - - - - repositories - - - - - - - - ArticleActiveRecord.php - - - - - - - - ArticleDbRepository.php - - - - - - ArticleService.php<\/code><\/pre>\n<h3>\u0421\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (AR \u0438 DTO)<\/h3>\n<p>\u041f\u0440\u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (Active Record \u0438 DTO) \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u043e\u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430\u043c\u0438, \u043e\u043d\u0438 \u043d\u0430\u043c \u043f\u0440\u0438\u0433\u043e\u0434\u044f\u0442\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0432 \u0432\u0438\u0434\u0435 \u0441\u0442\u0440\u043e\u043a, \u043f\u043b\u044e\u0441 \u043f\u0440\u0438 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433\u0435 \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u0432\u0441\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0414\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e.<\/p>\n<p>\u0412\u0430\u0436\u043d\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435, \u0447\u0442\u043e \u0432 Active Record \u0438\u043c\u0435\u043d\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 = \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0432 SnakeCase), \u0430 \u0432 DTO \u0438\u043c\u0435\u043d\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0432 lowerCamelCase.<\/p>\n<p><strong>ArticleActiveRecord.php<\/strong><\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article\\repositories;  \/**  * @property int    $id         \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440  * @property string $title      \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435  * @property string $text       \u0422\u0435\u043a\u0441\u0442 \u0441\u0442\u0430\u0442\u044c\u0438  * @property string $created_at \u0414\u0430\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f  *\/ class ArticleActiveRecord extends \\yii\\db\\ActiveRecord {     public const ATTR_ID         = 'id';     public const ATTR_TITLE      = 'title';     public const ATTR_TEXT       = 'text';     public const ATTR_CREATED_AT = 'created_at';      \/**      * @inheritDoc      *\/     public static function tableName(): string     {         return '{{%articles}}';     } }<\/code><\/pre>\n<p><strong>ArticleDto.php<\/strong><\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article\\dtos;  class ArticleDto {     public const ATTR_ID         = 'id';     public const ATTR_TITLE      = 'title';     public const ATTR_TEXT       = 'text';     public const ATTR_CREATED_AT = 'createdAt';      public function __construct(         readonly public int $id,         readonly public string $title,         readonly public string $text,         readonly public \\DateTimeInterface $createdAt     ) {     } }<\/code><\/pre>\n<h3>\u0421\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c<\/h3>\n<p><strong>ArticleDtoBuilder.php<\/strong>. \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c DTO \u0438\u0437 Active Record \u043e\u0431\u044a\u0435\u043a\u0442\u0430(\u043e\u0432).<\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article\\builders;  use app\\services\\article\\dtos\\ArticleDto; use app\\services\\article\\repositories\\ArticleActiveRecord;  class ArticleDtoBuilder {     public static function buildFromActiveRecord(ArticleActiveRecord $activeRecord): ArticleDto     {         return new ArticleDto(             $activeRecord->id,             $activeRecord->title,             $activeRecord->text,             new \\DateTimeImmutable($activeRecord->created_at),         );     }      \/**      * @param ArticleActiveRecord[] $activeRecords      *      * @return ArticleDto[]      *\/     public static function buildFromActiveRecords(array $activeRecords): array     {         $dtos = [];         foreach ($activeRecords as $activeRecord) {             if (!($activeRecord instanceof ArticleActiveRecord)) {                 continue;             }             $dtos[] = self::buildFromActiveRecord($activeRecord);         }                  return $dtos;     } }<\/code><\/pre>\n<h3>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/h3>\n<p><strong>ArticleDbRepository.php<\/strong>. \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044e, \u0432 \u043d\u0451\u043c \u043a\u0430\u043a \u0440\u0430\u0437 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u0417\u0430\u0434\u0430\u0435\u043c \u043a\u0430\u0440\u0442\u0443 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0433\u0434\u0435 \u043a\u043b\u044e\u0447\u0438 &#8212; \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0438\u0437 DTO. \u0422\u0430\u043a \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u0435\u0435 \u0432 \u043d\u0430\u0448\u0438\u0445 \u0432\u0438\u0434\u0436\u0435\u0442\u0430\u0445 \u0438\u043c\u0435\u043d\u043d\u043e \u043a \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c DTO, \u0430 \u043d\u0435 Active Record. \u042d\u0442\u043e \u0442\u0430\u043a \u0436\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u0438\u0434\u0436\u0435\u0442\u0443 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432 DTO, \u0430 \u043d\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446 \u0411\u0414 \u0438 \u0432 \u043a\u043b\u0430\u0441\u0441 <code>Sort<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0438\u0445, \u0430 \u043e\u043d \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0430\u043d\u043d\u043e\u0439 \u043a\u0430\u0440\u0442\u044b \u0441\u0430\u043c \u043f\u043e\u0439\u043c\u0435\u0442, \u0447\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 SQL \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<\/li>\n<li>\n<p>\u041c\u044b \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 AR \u043e\u0431\u044a\u0435\u043a\u0442\u044b (\u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0438, \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u043f\u0440.) \u043d\u0430 \u043d\u0430\u0448\u0438 DTO \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article\\repositories;  use app\\services\\article\\builders\\ArticleDtoBuilder; use app\\services\\article\\dtos\\ArticleDto; use yii\\data\\ActiveDataProvider;  class ArticleRepository {     public function findAllAsDataProvider(int $pageSize = 20): ActiveDataProvider     {         # \u0421\u043e\u0437\u0434\u0430\u0435\u043c Data Provider \u0441 \u043a\u0430\u0440\u0442\u043e\u0439 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438         $dataProvider = new ActiveDataProvider([             'query'      => ArticleActiveRecord::find(),             'pagination' => [                 'pageSize' => $pageSize ?: false,             ],             'sort'       => [                 'defaultOrder' => [ArticleDto::ATTR_CREATED_AT => SORT_DESC],                 'attributes'   => [                     ArticleDto::ATTR_ID           => [                         'asc'     => [ArticleActiveRecord::ATTR_ID => SORT_ASC],                         'desc'    => [ArticleActiveRecord::ATTR_ID => SORT_DESC],                         'default' => SORT_ASC,                     ],                     ArticleDto::ATTR_TITLE      => [                         'asc'     => [ArticleActiveRecord::ATTR_TITLE => SORT_ASC],                         'desc'    => [ArticleActiveRecord::ATTR_TITLE => SORT_DESC],                         'default' => SORT_ASC,                     ],                     ArticleDto::ATTR_TEXT        => [                         'asc'     => [ArticleActiveRecord::ATTR_TEXT => SORT_ASC],                         'desc'    => [ArticleActiveRecord::ATTR_TEXT => SORT_DESC],                         'default' => SORT_ASC,                     ],                     ArticleDto::ATTR_CREATED_AT => [                         'asc'     => [ArticleActiveRecord::ATTR_CREATED_AT => SORT_ASC],                         'desc'    => [ArticleActiveRecord::ATTR_CREATED_AT => SORT_DESC],                         'default' => SORT_ASC,                     ],                 ],             ],         ]);          $dataProvider->setModels(ArticleDtoBuilder::buildFromActiveRecords($dataProvider->getModels()));          return $dataProvider;     } }<\/code><\/pre>\n<h3>\u0421\u0435\u0440\u0432\u0438\u0441<\/h3>\n<p><strong>ArticleService.php<\/strong>. \u041a\u043e\u0434 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0430\u043c \u043d\u0435 \u0432\u0430\u0436\u0435\u043d \u0438 \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u043e\u0431\u0440\u0430\u0442\u0438\u043c\u0441\u044f \u043a \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044e \u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u041f\u043b\u044e\u0441 \u0438\u043c\u0435\u043d\u043d\u043e \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f (\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u043f\u043b\u044e\u0441 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0442 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0441\u043b\u043e\u044f \u0438 \u043f\u0440.).<\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article;  use app\\services\\article\\repositories\\ArticleRepository; use yii\\data\\ActiveDataProvider;  class ArticleService {     public function __construct(         readonly private ArticleRepository $repository     ) {     }      public function getAllAsDataProvider(): ActiveDataProvider     {         \/\/ \u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0412 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c.         return $this->repository->findAllAsDataProvider();     } }<\/code><\/pre>\n<h3>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440<\/h3>\n<p><strong>ArticleController.php<\/strong>. \u0418\u043c\u0435\u0435\u0442 3 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u0432\u0438\u0434\u0436\u0435\u0442\u043e\u0432. \u0414\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f DeatilView \u0432\u0438\u0434\u0436\u0435\u0442\u0430, \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043f\u0440\u044f\u043c \u0438\u0437 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\controllers;  use app\\services\\article\\ArticleService; use app\\services\\article\\dtos\\ArticleDto; use yii\\data\\ActiveDataProvider; use yii\\web\\Controller;  class ArticleController extends Controller {     public function __construct($id, $module, readonly private ArticleService $articleService, $config = [])     {         parent::__construct($id, $module, $config);     }      public function actionGrid(): string     {         return $this->render('grid', ['dataProvider' => $this->getDataProvider()]);     }      public function actionList(): string     {         return $this->render('list', ['dataProvider' => $this->getDataProvider()]);     }      public function actionDetail(): string     {         \/** @var ArticleDto[] $articles *\/         $articles = $this->getDataProvider()->getModels();          return $this->render('detail', ['article' => array_shift($articles)]);     }      private function getDataProvider(): ActiveDataProvider     {         return $this->articleService->getAllAsDataProvider();     } }<\/code><\/pre>\n<h2>\u0412\u0438\u0434\u0436\u0435\u0442\u044b<\/h2>\n<h3>GridView<\/h3>\n<p><strong>app\/views\/grid.php<\/strong> (Controller::actionGrid())<\/p>\n<p>\u0412 <strong>$dataProvider<\/strong> \u0443 \u043d\u0430\u0441 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043d\u0430\u0448 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440 \u0441 \u043d\u0430\u0448\u0438\u043c\u0438 DTO. \u0412 \u0432\u0438\u0434\u0436\u0435\u0442\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043e\u043f\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0438\u043c\u0435\u043d\u043d\u043e DTO \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043e\u0431 Active Record. \u041a\u043e\u0433\u0434\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0447\u0442\u043e-\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u0434 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c, \u0442\u043e \u0432 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 <code>ArticleDto<\/code>.<\/p>\n<pre><code class=\"php\">&lt;?php  use app\\services\\article\\dtos\\ArticleDto; use yii\\data\\ActiveDataProvider; use yii\\grid\\GridView; use yii\\helpers\\StringHelper; use yii\\web\\View;  \/**  * @var View               $this  * @var ActiveDataProvider $dataProvider  *\/ ?>  &lt;?= GridView::widget([     'dataProvider' => $dataProvider,     'columns'      => [         [             'attribute' => ArticleDto::ATTR_ID,             'label'     => Yii::t('app', 'ID'),         ],         [             'attribute' => ArticleDto::ATTR_TITLE,             'label'     => Yii::t('app', '\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a'),             'format'    => 'raw',             'value'     => function (ArticleDto $article) {                 return StringHelper::truncate($article->title, 50);             },         ],         [             'attribute' => ArticleDto::ATTR_TEXT,             'label'     => Yii::t('app', '\u0422\u0435\u043a\u0441\u0442'),             'format'    => 'raw',             'value'     => function (ArticleDto $article) {                 return StringHelper::truncate($article->title, 200);             },         ],         [             'attribute' => ArticleDto::ATTR_CREATED_AT,             'label'     => Yii::t('app', '\u0414\u0430\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f'),             'value'     => function (ArticleDto $article) {                 return $article->createdAt->format('Y-m-d');             },         ],     ], ]); ?><\/code><\/pre>\n<h3>ListView<\/h3>\n<p><strong>app\/views\/list.php<\/strong>  (Controller::actionList())<\/p>\n<p>\u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0442\u0430\u043a\u0438\u0435 \u0436\u0435, \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043b\u0438\u0448\u044c \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0430\u043c\u043e\u0433\u043e \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \u0438 \u0447\u0442\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0435\u0435 \u0437\u0430 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0443 1 \u0437\u0430\u043f\u0438\u0441\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f DTO.<\/p>\n<pre><code class=\"php\">&lt;?php  use yii\\data\\ActiveDataProvider; use yii\\web\\View; use yii\\widgets\\ListView;  \/**  * @var View               $this  * @var ActiveDataProvider $dataProvider  *\/ ?>  &lt;?= ListView::widget([     'dataProvider' => $dataProvider,     'itemView'     => 'list_item', ]); ?><\/code><\/pre>\n<p><strong>app\/views\/list_item.php<\/strong><\/p>\n<pre><code class=\"php\">&lt;?php  use app\\services\\article\\dtos\\ArticleDto; use yii\\web\\View;  \/**  * @var View       $this  * @var ArticleDto $model  *\/ ?>  &lt;h1>&lt;?= $model->title ?>&lt;\/h1> &lt;div>&lt;?= $model->text ?>&lt;\/div><\/code><\/pre>\n<h3>DetailView<\/h3>\n<p><strong>app\/views\/detail.php<\/strong>  (Controller::actionDetail())<\/p>\n<p>\u0412 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f DTO \u0438 \u044d\u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0432\u0438\u0434\u0436\u0435\u0442 (\u043a\u0430\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <strong>model<\/strong>).<\/p>\n<pre><code class=\"php\">&lt;?php  use app\\services\\article\\dtos\\ArticleDto; use yii\\web\\View; use yii\\widgets\\DetailView;  \/**  * @var View       $this  * @var ArticleDto $article  *\/ ?>  &lt;?= DetailView::widget([     'model'      => $article,     'attributes' => [         [             'attribute' => ArticleDto::ATTR_ID,             'label'     => Yii::t('app', '\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440'),         ],         ArticleDto::ATTR_TITLE,         ArticleDto::ATTR_TEXT . ':html',         [             'label' => Yii::t('app', '\u0414\u0430\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f'),             'value' => $article->createdAt->format('Y-m-d'),         ],     ], ]) ?><\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433<\/h2>\n<p>\u041a\u043e\u0440\u043e\u0442\u043a\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f: <\/p>\n<ul>\n<li>\n<p>\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u0441\u0435 Active Record \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u0448\u0438\u043c\u0438 DTO<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u0430\u0440\u0442\u0443 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (GridView) \u0438 \u0434\u043b\u044f \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0432\u0438\u0434\u0436\u0435\u0442\u0430\u0445 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432 DTO, \u0430 \u043d\u0435 Active Record<\/p>\n<\/li>\n<li>\n<p>\u0432 \u0432\u0438\u0434\u0436\u0435\u0442\u0430\u0445 \u043f\u0440\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0439 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 DTO<\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/677408\/\"> https:\/\/habr.com\/ru\/post\/677408\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0411\u0430\u0437\u043e\u0432\u043e Yii2 \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u043d\u0430\u043c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0443 MVC (\u043c\u043e\u0434\u0435\u043b\u044c, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440). \u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0435\u043c \u043a \u0447\u0438\u0441\u0442\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 (\u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/habr.com\/ru\/post\/269589\/\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e<\/a> \u0434\u043b\u044f \u043e\u0431\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f) \u0438 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043d\u0435\u0451 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0442 Active Record \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u0445 (\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u0445), \u0442.\u043a. AR \u044d\u0442\u043e \u0447\u0430\u0441\u0442\u044c \u0441\u043b\u043e\u044f \u043f\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u0440\u0443\u0433\u0438\u043c \u0441\u043b\u043e\u044f\u043c \u0437\u043d\u0430\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0432\u0438\u0434\u0436\u0435\u0442\u044b \u043f\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u0445: <code>DeatilView<\/code>, <code>ListView<\/code> \u0438 <code>GridView<\/code>. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0434\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 <code>ActiveDataProvider<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0441\u0435\u0431\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 Active Record \u043c\u043e\u0434\u0435\u043b\u0438 &#8212; \u0446\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0438\u0445 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e DTO.<\/p>\n<h2>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h2>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0432 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u043a\u043e\u0434\u0443.<\/p>\n<p>\u041f\u0443\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<ol>\n<li>\n<p>\u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0438\u0445 (\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u0430\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f) \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u0432 \u0441\u0435\u0440\u0432\u0438\u0441<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043d\u0435\u043a\u0443\u044e \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0411\u0414, \u043e\u043d \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044e<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 SQL \u0437\u0430\u043f\u0440\u043e\u0441, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0443\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0435\u0442 \u0432 DTO \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0435\u0440\u0432\u0438\u0441\u0443<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u0441\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0443<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f View Render (\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f) \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 HTML \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u043f\u0443\u043d\u043a\u0442\u0435 6, \u043c\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 <code>ActiveDataProvider<\/code> (\u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043b\u044e\u0431\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 <code>DataProviderInterface<\/code>), \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0445\u0440\u0430\u043d\u044f\u0449\u0438\u0435\u0441\u044f \u0432 \u043d\u0451\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u044d\u0442\u043e \u043d\u0435 Active Record \u043c\u043e\u0434\u0435\u043b\u0438, \u0430 Data Transfer Object (DTO).<\/p>\n<p>\u0421\u043b\u043e\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<ol>\n<li>\n<p>User Interface: \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 (\u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0438\u0445, \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0432 \u0441\u0435\u0440\u0432\u0438\u0441, \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f)<\/p>\n<\/li>\n<li>\n<p>Business Logic: \u0441\u0435\u0440\u0432\u0438\u0441\u044b \u0441 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u043e\u0439<\/p>\n<\/li>\n<li>\n<p>Data Access: \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 (\u0447\u0430\u0441\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0442\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043f\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 Active Record)<\/p>\n<\/li>\n<\/ol>\n<h2>\u041a\u043e\u0434<\/h2>\n<h3>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430<\/h3>\n<p><em>\u0414\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f, \u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0430 \u043d\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u0435\u0436\u0434\u0443 \u043a\u0430\u0436\u0434\u044b\u043c \u0441\u043b\u043e\u044f\u043c, \u044d\u0442\u043e \u0441\u0432\u043e\u0439 DTO \u0438 \u043f\u0440.).<\/em><\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 &#171;\u0431\u043b\u043e\u0433&#187; \u0441\u043e \u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438.<\/p>\n<p>\u0424\u0430\u0439\u043b\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430:<\/p>\n<pre><code>app - - controllers - - - - ArticleController.php - - views - - - - article - - - - - - grid.php - - - - - - list.php - - - - - - list_item.php - - - - - - detail.php - - services - - - - article - - - - - - builders - - - - - - - - ArticleDtoBuilder.php - - - - - - dtos - - - - - - - - ArticleDto.php - - - - - - repositories - - - - - - - - ArticleActiveRecord.php - - - - - - - - ArticleDbRepository.php - - - - - - ArticleService.php<\/code><\/pre>\n<h3>\u0421\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (AR \u0438 DTO)<\/h3>\n<p>\u041f\u0440\u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 (Active Record \u0438 DTO) \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u043e\u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430\u043c\u0438, \u043e\u043d\u0438 \u043d\u0430\u043c \u043f\u0440\u0438\u0433\u043e\u0434\u044f\u0442\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0432 \u0432\u0438\u0434\u0435 \u0441\u0442\u0440\u043e\u043a, \u043f\u043b\u044e\u0441 \u043f\u0440\u0438 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433\u0435 \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u0432\u0441\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0414\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e.<\/p>\n<p>\u0412\u0430\u0436\u043d\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435, \u0447\u0442\u043e \u0432 Active Record \u0438\u043c\u0435\u043d\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 = \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0432 SnakeCase), \u0430 \u0432 DTO \u0438\u043c\u0435\u043d\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0432 lowerCamelCase.<\/p>\n<p><strong>ArticleActiveRecord.php<\/strong><\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article\\repositories;  \/**  * @property int    $id         \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440  * @property string $title      \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435  * @property string $text       \u0422\u0435\u043a\u0441\u0442 \u0441\u0442\u0430\u0442\u044c\u0438  * @property string $created_at \u0414\u0430\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f  *\/ class ArticleActiveRecord extends \\yii\\db\\ActiveRecord {     public const ATTR_ID         = 'id';     public const ATTR_TITLE      = 'title';     public const ATTR_TEXT       = 'text';     public const ATTR_CREATED_AT = 'created_at';      \/**      * @inheritDoc      *\/     public static function tableName(): string     {         return '{{%articles}}';     } }<\/code><\/pre>\n<p><strong>ArticleDto.php<\/strong><\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article\\dtos;  class ArticleDto {     public const ATTR_ID         = 'id';     public const ATTR_TITLE      = 'title';     public const ATTR_TEXT       = 'text';     public const ATTR_CREATED_AT = 'createdAt';      public function __construct(         readonly public int $id,         readonly public string $title,         readonly public string $text,         readonly public \\DateTimeInterface $createdAt     ) {     } }<\/code><\/pre>\n<h3>\u0421\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c<\/h3>\n<p><strong>ArticleDtoBuilder.php<\/strong>. \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c DTO \u0438\u0437 Active Record \u043e\u0431\u044a\u0435\u043a\u0442\u0430(\u043e\u0432).<\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article\\builders;  use app\\services\\article\\dtos\\ArticleDto; use app\\services\\article\\repositories\\ArticleActiveRecord;  class ArticleDtoBuilder {     public static function buildFromActiveRecord(ArticleActiveRecord $activeRecord): ArticleDto     {         return new ArticleDto(             $activeRecord->id,             $activeRecord->title,             $activeRecord->text,             new \\DateTimeImmutable($activeRecord->created_at),         );     }      \/**      * @param ArticleActiveRecord[] $activeRecords      *      * @return ArticleDto[]      *\/     public static function buildFromActiveRecords(array $activeRecords): array     {         $dtos = [];         foreach ($activeRecords as $activeRecord) {             if (!($activeRecord instanceof ArticleActiveRecord)) {                 continue;             }             $dtos[] = self::buildFromActiveRecord($activeRecord);         }                  return $dtos;     } }<\/code><\/pre>\n<h3>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/h3>\n<p><strong>ArticleDbRepository.php<\/strong>. \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044e, \u0432 \u043d\u0451\u043c \u043a\u0430\u043a \u0440\u0430\u0437 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u0417\u0430\u0434\u0430\u0435\u043c \u043a\u0430\u0440\u0442\u0443 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0433\u0434\u0435 \u043a\u043b\u044e\u0447\u0438 &#8212; \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0438\u0437 DTO. \u0422\u0430\u043a \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u0435\u0435 \u0432 \u043d\u0430\u0448\u0438\u0445 \u0432\u0438\u0434\u0436\u0435\u0442\u0430\u0445 \u0438\u043c\u0435\u043d\u043d\u043e \u043a \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c DTO, \u0430 \u043d\u0435 Active Record. \u042d\u0442\u043e \u0442\u0430\u043a \u0436\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u0438\u0434\u0436\u0435\u0442\u0443 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432 DTO, \u0430 \u043d\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446 \u0411\u0414 \u0438 \u0432 \u043a\u043b\u0430\u0441\u0441 <code>Sort<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0438\u0445, \u0430 \u043e\u043d \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0430\u043d\u043d\u043e\u0439 \u043a\u0430\u0440\u0442\u044b \u0441\u0430\u043c \u043f\u043e\u0439\u043c\u0435\u0442, \u0447\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 SQL \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<\/li>\n<li>\n<p>\u041c\u044b \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 AR \u043e\u0431\u044a\u0435\u043a\u0442\u044b (\u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0438, \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u043f\u0440.) \u043d\u0430 \u043d\u0430\u0448\u0438 DTO \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article\\repositories;  use app\\services\\article\\builders\\ArticleDtoBuilder; use app\\services\\article\\dtos\\ArticleDto; use yii\\data\\ActiveDataProvider;  class ArticleRepository {     public function findAllAsDataProvider(int $pageSize = 20): ActiveDataProvider     {         # \u0421\u043e\u0437\u0434\u0430\u0435\u043c Data Provider \u0441 \u043a\u0430\u0440\u0442\u043e\u0439 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438         $dataProvider = new ActiveDataProvider([             'query'      => ArticleActiveRecord::find(),             'pagination' => [                 'pageSize' => $pageSize ?: false,             ],             'sort'       => [                 'defaultOrder' => [ArticleDto::ATTR_CREATED_AT => SORT_DESC],                 'attributes'   => [                     ArticleDto::ATTR_ID           => [                         'asc'     => [ArticleActiveRecord::ATTR_ID => SORT_ASC],                         'desc'    => [ArticleActiveRecord::ATTR_ID => SORT_DESC],                         'default' => SORT_ASC,                     ],                     ArticleDto::ATTR_TITLE      => [                         'asc'     => [ArticleActiveRecord::ATTR_TITLE => SORT_ASC],                         'desc'    => [ArticleActiveRecord::ATTR_TITLE => SORT_DESC],                         'default' => SORT_ASC,                     ],                     ArticleDto::ATTR_TEXT        => [                         'asc'     => [ArticleActiveRecord::ATTR_TEXT => SORT_ASC],                         'desc'    => [ArticleActiveRecord::ATTR_TEXT => SORT_DESC],                         'default' => SORT_ASC,                     ],                     ArticleDto::ATTR_CREATED_AT => [                         'asc'     => [ArticleActiveRecord::ATTR_CREATED_AT => SORT_ASC],                         'desc'    => [ArticleActiveRecord::ATTR_CREATED_AT => SORT_DESC],                         'default' => SORT_ASC,                     ],                 ],             ],         ]);          $dataProvider->setModels(ArticleDtoBuilder::buildFromActiveRecords($dataProvider->getModels()));          return $dataProvider;     } }<\/code><\/pre>\n<h3>\u0421\u0435\u0440\u0432\u0438\u0441<\/h3>\n<p><strong>ArticleService.php<\/strong>. \u041a\u043e\u0434 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0430\u043c \u043d\u0435 \u0432\u0430\u0436\u0435\u043d \u0438 \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u043e\u0431\u0440\u0430\u0442\u0438\u043c\u0441\u044f \u043a \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044e \u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u041f\u043b\u044e\u0441 \u0438\u043c\u0435\u043d\u043d\u043e \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f (\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u043f\u043b\u044e\u0441 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0442 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0441\u043b\u043e\u044f \u0438 \u043f\u0440.).<\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\services\\article;  use app\\services\\article\\repositories\\ArticleRepository; use yii\\data\\ActiveDataProvider;  class ArticleService {     public function __construct(         readonly private ArticleRepository $repository     ) {     }      public function getAllAsDataProvider(): ActiveDataProvider     {         \/\/ \u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0412 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c.         return $this->repository->findAllAsDataProvider();     } }<\/code><\/pre>\n<h3>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440<\/h3>\n<p><strong>ArticleController.php<\/strong>. \u0418\u043c\u0435\u0435\u0442 3 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u0432\u0438\u0434\u0436\u0435\u0442\u043e\u0432. \u0414\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f DeatilView \u0432\u0438\u0434\u0436\u0435\u0442\u0430, \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043f\u0440\u044f\u043c \u0438\u0437 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"php\">&lt;?php  namespace app\\controllers;  use app\\services\\article\\ArticleService; use app\\services\\article\\dtos\\ArticleDto; use yii\\data\\ActiveDataProvider; use yii\\web\\Controller;  class ArticleController extends Controller {     public function __construct($id, $module, readonly private ArticleService $articleService, $config = [])     {         parent::__construct($id, $module, $config);     }      public function actionGrid(): string     {         return $this->render('grid', ['dataProvider' => $this->getDataProvider()]);     }      public function actionList(): string     {         return $this->render('list', ['dataProvider' => $this->getDataProvider()]);     }      public function actionDetail(): string     {         \/** @var ArticleDto[] $articles *\/         $articles = $this->getDataProvider()->getModels();          return $this->render('detail', ['article' => array_shift($articles)]);     }      private function getDataProvider(): ActiveDataProvider     {         return $this->articleService->getAllAsDataProvider();     } }<\/code><\/pre>\n<h2>\u0412\u0438\u0434\u0436\u0435\u0442\u044b<\/h2>\n<h3>GridView<\/h3>\n<p><strong>app\/views\/grid.php<\/strong> (Controller::actionGrid())<\/p>\n<p>\u0412 <strong>$dataProvider<\/strong> \u0443 \u043d\u0430\u0441<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-335810","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=335810"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335810\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}