{"id":1094,"date":"2021-11-12T10:05:09","date_gmt":"2021-11-12T10:05:09","guid":{"rendered":"https:\/\/hmmh.pl\/?p=1094"},"modified":"2024-10-08T12:37:10","modified_gmt":"2024-10-08T12:37:10","slug":"shopware-6-custom-module-criteria","status":"publish","type":"post","link":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/","title":{"rendered":"Shopware 6 &#8211; Custom module Criteria"},"content":{"rendered":"\n<p><strong>Tworz\u0105c wtyczk\u0119 dla naszego klienta, opr\u00f3cz pracy w PHP, konieczne by\u0142o napisanie dodatkowego modu\u0142u z wykorzystaniem Vue &#8211; frontendowego frameworku. Wynika\u0142o to z konieczno\u015bci wy\u015bwietlania kilku encji z bazy danych w odpowiedni spos\u00f3b &#8211; posortowanych, zgrupowanych w odpowiednim zakresie datowym. Najpierw jednak skierowali\u015bmy si\u0119 w stron\u0119 dokumentacji Shopware. Dokumentacja klasy \u201cCriteria\u201d dla Vue oraz dla PHP zawiera te same metody, ale ich u\u017cycie oraz sk\u0142adnia r\u00f3\u017cni\u0105 si\u0119 od siebie. Dlatego te\u017c poni\u017cej opisali\u015bmy zastosowanie metod tej biblioteki w Vue.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tworzenie nowych modu\u0142\u00f3w w Vue w Shopware 6<\/h2>\n\n\n\n<p>Tworz\u0105c nowy modu\u0142 w Vue cz\u0119sto \u0142\u0105czymy si\u0119 z baz\u0105 danych pobieraj\u0105c dane, kt\u00f3re nast\u0119pnie wy\u015bwietlamy lub odpowiednio wcze\u015bniej przetwarzamy. Do ograniczenia wyszukiwanych danych w Shopware s\u0142u\u017cy klasa \u201cCriteria\u201d. Dzi\u0119ki niemu mamy niemal nieograniczone mo\u017cliwo\u015bci filtrowania, ale r\u00f3wnie\u017c rozszerzania potrzebnych danych jeszcze przed wykonaniem po\u0142\u0105czenia z baz\u0105 danych. Po stronie backendu (PHP) w dokumentacji znajdziemy rozpisane wszystkie mo\u017cliwo\u015bci tej biblioteki. Znajdziemy j\u0105 pod <a href=\"https:\/\/developer.shopware.com\/docs\/guides\/plugins\/plugins\/framework\/data-handling\/reading-data\" target=\"_blank\" rel=\"noreferrer noopener\">linkiem<\/a> . W tym artykule chcia\u0142bym przybli\u017cy\u0107 t\u0119 bibliotek\u0119 odwzorowan\u0105 po stronie Vue.<\/p>\n\n\n\n<div style=\"display:flex; flex-direction:row; align-items:flex-start;margin-top:30px;margin-bottom:30px;\">\n<img decoding=\"async\" src=\"https:\/\/hmmh.pl\/app\/uploads\/2021\/11\/1585641687473.jpeg\" style=\"border-radius:150px; width:120px;box-shadow:none\">\n<div style=\"display:flex; flex-direction:column; align-items:flex-end; padding: 0 20px;\">\n<blockquote style=\"border-left: 1px solid #d3d3d3;font-size:14px; font-style:italic\">Warto zauwa\u017cy\u0107 i doceni\u0107 intencj\u0119 tw\u00f3rc\u00f3w Shopware, kt\u00f3rzy zastosowali ten sam interfejs dost\u0119pu do danych po dw\u00f3ch stronach systemu, dzi\u0119ki czemu mo\u017cna szybko prze\u0142\u0105cza\u0107 si\u0119 pomi\u0119dzy frontendem i backendem bez konieczno\u015bci przestawiania mindsetu.<\/blockquote>\n<p style=\"font-weight:bold text-align:center;margin-bottom:5px;\">Bogdan Jakubowski<\/p>\n<p style=\"color: #bababa; text-align:right;\">Senior PHP Developer, hmmh Poland<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>Tworz\u0105c nowy plik index.js w module, a nast\u0119pnie do\u0142\u0105czaj\u0105c do niego serwis \u201cCriteria\u201d, mo\u017ce on wygl\u0105da\u0107 w spos\u00f3b jak w ramce poni\u017cej:<\/p>\n\n\n\n<div style=\"background:black;color:white;padding:20px;margin-bottom:20px;\">\n<pre>import template from '.\/example-module.html.twig';\n\nconst { Component } = Shopware;\nconst { Criteria } = Shopware.Data;\n\nComponent.register('example-module', {\n   template,\n\n   inject: [\n       'repositoryFactory'\n   ],\n\n   data() {\n       return {\n           productRepository: null,\n       };\n   },\n\n   created() {  this.productRepository = this.repositoryFactory.create('product');\n   },\n  \n   methods: {\n       getData()\n       {\n           const criteria = new Criteria();\n\n           this.productRepository\n               .search(criteria, Shopware.Context.api)\n               .then((result) =&gt; {\n                   \/\/...\n               });\n       }\n   }\n});\n\n<\/pre>\n<\/div>\n\n\n\n<p>Na potrzeby artyku\u0142u skupimy si\u0119 tylko na funkcji getData. Poni\u017cej przedstawione zosta\u0142y wszystkie mo\u017cliwo\u015bci serwisu \u201cCriteria\u201d wraz z przyk\u0142adowymi zastosowaniami.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Paginacja<\/h2>\n\n\n\n<p>Paginacja najcz\u0119\u015bciej jest u\u017cywana z komponentem `&lt;sw-data-grid`. Dzi\u0119ki niej mo\u017cemy sterowa\u0107 stron\u0105 wynik\u00f3w (this.page) oraz liczb\u0105 wynik\u00f3w na stron\u0119 (this.limit). Mo\u017cemy r\u00f3wnie\u017c u\u017cy\u0107 odpowiednio metod `setPage` oraz `setLimit`, zamiast wprowadza\u0107 dane bezpo\u015brednio w konstruktor klasy.<\/p>\n\n\n\n<div style=\"background:black;color:white;padding:20px;margin-bottom:20px;\">\n<pre>getData()\n       {\n           const criteria = new Criteria(this.page, this.limit)\n           \/\/...\n       }\n<\/pre>\n<\/div>\n\n\n\n<div style=\"background:black;color:white;padding:20px;margin-bottom:20px;\">\n<pre>getData()\n       {\n           const criteria = new Criteria();\n\n           const criteria.setPage(this.page);\n\n           const criteria.setLimit(this.limit);\n\n           \/\/...<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Ograniczenie do listy ID<\/h2>\n\n\n\n<p>Je\u015bli chcemy ograniczy\u0107 wyniki tylko do konkretnych ID, mo\u017cemy u\u017cy\u0107 metody `setIds`.<\/p>\n\n\n\n<div style=\"background:black;color:white;padding:20px;margin-bottom:20px;\">\n<pre>getData()\n       {\n           const criteria = new Criteria();\n\n           criteria.setIds([id1,id2,id3]);\n\n           \/\/...\n       }    \n<\/pre>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wyszukiwanie wyraz\u00f3w<\/h2>\n\n\n\n<p>Szukaj\u0105c konkretnego ci\u0105gu znak\u00f3w, u\u017cyjemy metody setTerm(). Dzi\u0119ki niej wyszukamy dane na zasadzie LIKE &#8216;%or%&#8217; &#8211; znajduje warto\u015bci, kt\u00f3re maj\u0105 \u201eor\u201d w dowolnej pozycji.<\/p>\n\n\n\n<div style=\"background:black;color:white;padding:20px;margin-bottom:20px;\">\n<pre>       getData()\n       {\n           const criteria = new Criteria();\n\n           criteria.setTerm(\u201cexample\u201d);\n\n           \/\/...\n       } \n<\/pre>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Szczeg\u00f3\u0142owe filtrowanie wynik\u00f3w<\/h2>\n\n\n\n<p>Pobieraj\u0105c dane z bazy danych cz\u0119sto chcemy wprowadzi\u0107 kilka wytycznych, aby dok\u0142adnie okre\u015bli\u0107, jakich danych potrzebujemy. Je\u015bli chodzi o Shopware, to przychodzi nam z pomoc\u0105 funkcja addFilter(). Do tej metody \u0142\u0105czy si\u0119 bardzo du\u017ca liczba filtr\u00f3w, dzi\u0119ki kt\u00f3rym mo\u017cemy w maksymalnym stopniu wprowadzi\u0107 nasze warunki.<\/p>\n\n\n\n<div style=\"background:black;color:white;padding:20px;margin-bottom:20px;\">\n<pre>       getData()\n       {\n           const criteria = new Criteria();\n\n           criteria.addFilter(\n              Criteria.equals(\/\/...\/\/)\n           );\n\n           criteria.addFilter(\n              Criteria.equalsAny(\/\/...\/\/)\n           );\n\n           criteria.addFilter(\n              Criteria.not(\/\/...\/\/)\n           );\n\n           criteria.addFilter(\n              Criteria.range(\/\/...\/\/)\n           );\n\n           criteria.addFilter(\n              Criteria.multi(\/\/...\/\/)\n           );\n\n           \/\/...\n       }\n<\/pre>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Sortowanie wynik\u00f3w<\/h2>\n\n\n\n<p>Zasada sortowania wynik\u00f3w ogranicza si\u0119 do wprowadzenia pola, po kt\u00f3rym wyniki maj\u0105 zosta\u0107 posortowane oraz typu sortowania (ASC &#8211; sortowanie rosn\u0105co lub DESC &#8211; sortowanie malej\u0105co).<\/p>\n\n\n\n<div style=\"background:black;color:white;padding:20px;margin-bottom:20px;\">\n<pre>     getData()\n       {\n           const criteria = new Criteria();\n\n           criteria.addSorting(Criteria.sort('name', 'ASC'));\n           \/\/...\n       }\n<\/pre>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Grupowanie wynik\u00f3w<\/h2>\n\n\n\n<p>Metoda \u2018addGroupField\u2019 grupuje wiersze, kt\u00f3re maj\u0105 te same warto\u015bci.<\/p>\n\n\n\n<div style=\"background:black;color:white;padding:20px;margin-bottom:20px;\">\n<pre>     getData()\n       {\n           const criteria = new Criteria();\n\n           criteria.addGroupField('columnName');\n\n           \/\/...\n       }\n<\/pre>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Dodawanie relacji<\/h2>\n\n\n\n<p>Je\u015bli w definicji encji utworzyli\u015bmy relacj\u0119 mi\u0119dzy innymi encjami, mo\u017cemy j\u0105 \u0142atwo do\u0142\u0105czy\u0107 stosuj\u0105c funkcj\u0119 addAssociation(). W przyk\u0142adzie poni\u017cej do naszych wynik\u00f3w zostanie do\u0142\u0105czona encja \u201cProdukt\u201d. Mo\u017cemy tworzy\u0107 wiele relacji, o ile konstrukcja samej definicji na to pozwala.<\/p>\n\n\n\n<div style=\"background:black;color:white;padding:20px;margin-bottom:20px;\">\n<pre>     getData()\n       {\n           const criteria = new Criteria();\n\n           criteria.addAssociation('product');\n\n           \/\/...\n       }      \n<\/pre>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Podsumowanie<\/h2>\n\n\n\n<p>Biblioteka \u201cCriteria\u201d jest u\u017cywana w ka\u017cdym module Shopware wy\u015bwietlaj\u0105cym dane z bazy danych. Dzi\u0119ki niej mo\u017cemy sprecyzowa\u0107, jakie dane dok\u0142adnie chcemy, aby zosta\u0142y pobrane. Lista wszystkich metod biblioteki \u201cCrteria\u201d w JS mo\u017cemy znale\u017a\u0107 w pliku \u017ar\u00f3d\u0142owym Shopware pod <a href=\"http:\/\/platform\/src\/Administration\/Resources\/app\/administration\/src\/core\/data\/criteria.data.js.\" target=\"_blank\" rel=\"noreferrer noopener\">\u015bcie\u017ck\u0105.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tworz\u0105c wtyczk\u0119 dla naszego klienta, opr\u00f3cz pracy w PHP, konieczne by\u0142o napisanie dodatkowego modu\u0142u z<\/p>\n","protected":false},"author":7,"featured_media":5514,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[18],"tags":[67,66,71,64,63],"class_list":["post-1094","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-inne","tag-development","tag-e-commerce-3","tag-it-3","tag-shopware-3","tag-vue-3"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Shopware 6 - Custom module Criteria - Software house for E-commerce and Web development - Hmmh Poland<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/\" \/>\n<meta property=\"og:locale\" content=\"pl_PL\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Shopware 6 - Custom module Criteria - Software house for E-commerce and Web development - Hmmh Poland\" \/>\n<meta property=\"og:description\" content=\"Tworz\u0105c wtyczk\u0119 dla naszego klienta, opr\u00f3cz pracy w PHP, konieczne by\u0142o napisanie dodatkowego modu\u0142u z\" \/>\n<meta property=\"og:url\" content=\"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/\" \/>\n<meta property=\"og:site_name\" content=\"Software house for E-commerce and Web development - Hmmh Poland\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-12T10:05:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-08T12:37:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/hmmh.pl\/app\/uploads\/2021\/11\/IMG_3727.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"5472\" \/>\n\t<meta property=\"og:image:height\" content=\"3648\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Lukasz Bialonoga\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Napisane przez\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lukasz Bialonoga\" \/>\n\t<meta name=\"twitter:label2\" content=\"Szacowany czas czytania\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Shopware 6 - Custom module Criteria - Software house for E-commerce and Web development - Hmmh Poland","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/","og_locale":"pl_PL","og_type":"article","og_title":"Shopware 6 - Custom module Criteria - Software house for E-commerce and Web development - Hmmh Poland","og_description":"Tworz\u0105c wtyczk\u0119 dla naszego klienta, opr\u00f3cz pracy w PHP, konieczne by\u0142o napisanie dodatkowego modu\u0142u z","og_url":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/","og_site_name":"Software house for E-commerce and Web development - Hmmh Poland","article_published_time":"2021-11-12T10:05:09+00:00","article_modified_time":"2024-10-08T12:37:10+00:00","og_image":[{"width":5472,"height":3648,"url":"https:\/\/hmmh.pl\/app\/uploads\/2021\/11\/IMG_3727.jpg","type":"image\/jpeg"}],"author":"Lukasz Bialonoga","twitter_card":"summary_large_image","twitter_misc":{"Napisane przez":"Lukasz Bialonoga","Szacowany czas czytania":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/#article","isPartOf":{"@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/"},"author":{"name":"Lukasz Bialonoga","@id":"https:\/\/hmmh.pl\/#\/schema\/person\/c23f914aa1548cebb26526bd250898b8"},"headline":"Shopware 6 &#8211; Custom module Criteria","datePublished":"2021-11-12T10:05:09+00:00","dateModified":"2024-10-08T12:37:10+00:00","mainEntityOfPage":{"@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/"},"wordCount":619,"image":{"@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/#primaryimage"},"thumbnailUrl":"https:\/\/hmmh.pl\/app\/uploads\/2025\/01\/Shopware-6-Custom-module-Criteria.png","keywords":["#Development","#e-commerce","#IT","#Shopware","#VUE"],"articleSection":["Inne"],"inLanguage":"pl-PL"},{"@type":"WebPage","@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/","url":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/","name":"Shopware 6 - Custom module Criteria - Software house for E-commerce and Web development - Hmmh Poland","isPartOf":{"@id":"https:\/\/hmmh.pl\/#website"},"primaryImageOfPage":{"@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/#primaryimage"},"image":{"@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/#primaryimage"},"thumbnailUrl":"https:\/\/hmmh.pl\/app\/uploads\/2025\/01\/Shopware-6-Custom-module-Criteria.png","datePublished":"2021-11-12T10:05:09+00:00","dateModified":"2024-10-08T12:37:10+00:00","author":{"@id":"https:\/\/hmmh.pl\/#\/schema\/person\/c23f914aa1548cebb26526bd250898b8"},"breadcrumb":{"@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/#breadcrumb"},"inLanguage":"pl-PL","potentialAction":[{"@type":"ReadAction","target":["https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/"]}]},{"@type":"ImageObject","inLanguage":"pl-PL","@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/#primaryimage","url":"https:\/\/hmmh.pl\/app\/uploads\/2025\/01\/Shopware-6-Custom-module-Criteria.png","contentUrl":"https:\/\/hmmh.pl\/app\/uploads\/2025\/01\/Shopware-6-Custom-module-Criteria.png","width":1920,"height":1280},{"@type":"BreadcrumbList","@id":"https:\/\/hmmh.pl\/pl\/inne\/shopware-6-custom-module-criteria\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/hmmh.pl\/pl\/"},{"@type":"ListItem","position":2,"name":"Shopware 6 &#8211; Custom module Criteria"}]},{"@type":"WebSite","@id":"https:\/\/hmmh.pl\/#website","url":"https:\/\/hmmh.pl\/","name":"Software house for E-commerce and Web development - Hmmh Poland","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/hmmh.pl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pl-PL"},{"@type":"Person","@id":"https:\/\/hmmh.pl\/#\/schema\/person\/c23f914aa1548cebb26526bd250898b8","name":"Lukasz Bialonoga","image":{"@type":"ImageObject","inLanguage":"pl-PL","@id":"https:\/\/secure.gravatar.com\/avatar\/53f53188c7aaa22b4bbc57a964cad28a9d93fa10a8a89cf9a8280e1004d5cd64?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/53f53188c7aaa22b4bbc57a964cad28a9d93fa10a8a89cf9a8280e1004d5cd64?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/53f53188c7aaa22b4bbc57a964cad28a9d93fa10a8a89cf9a8280e1004d5cd64?s=96&d=mm&r=g","caption":"Lukasz Bialonoga"}}]}},"_links":{"self":[{"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/posts\/1094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/comments?post=1094"}],"version-history":[{"count":20,"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/posts\/1094\/revisions"}],"predecessor-version":[{"id":1473,"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/posts\/1094\/revisions\/1473"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/media\/5514"}],"wp:attachment":[{"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/media?parent=1094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/categories?post=1094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hmmh.pl\/pl\/wp-json\/wp\/v2\/tags?post=1094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}