{"id":2732,"date":"2018-08-15T21:23:32","date_gmt":"2018-08-15T19:23:32","guid":{"rendered":"https:\/\/nyamsprod.com\/blog\/?p=2732"},"modified":"2018-08-15T21:27:52","modified_gmt":"2018-08-15T19:27:52","slug":"using-doctrine-collections-to-improve-csv-filtering","status":"publish","type":"post","link":"https:\/\/nyamsprod.com\/blog\/using-doctrine-collections-to-improve-csv-filtering\/","title":{"rendered":"Using Doctrine Collections  to improve CSV filtering"},"content":{"rendered":"<div class=\"message warning\">\n<p><strong>Attention:<\/strong> Les informations de ce billet sont susceptibles d'&ecirc;tre obsol&egrave;tes car vieux de plus 2 ans.<\/p>\n<p><strong>Warning: <\/strong> The information you are reading may be obsolete, this post was published more than 2 years ago.<\/p>\n<\/div><p>I&#8217;m happy to announce the release of <a href=\"https:\/\/github.com\/bakame-php\/csv-doctrine-collections-bridge\">League CSV &#8211; Doctrine Collection Bridge&nbsp;<\/a>version 1.0.0. The new package under the bakame namespace is an extension to <a href=\"https:\/\/csv.thephpleague.com\/\">League\\Csv<\/a> which takes advantage of Doctrine Collections features to better manipulate CSV documents.<\/p>\n<h3>CSV Filtering<\/h3>\n<p>When working with imported CSV documents it always comes down to filtering and querying data from the document to apply some business logic to them. One of the often suggested method is to import the CSV document data into a RBMS and then use its filtering capabilities to process the data. While this might be tempting it also means wasting&nbsp; times in thinking about tables structure and resource availability&nbsp; while often you only want to consume your data and move on to other tasks.<\/p>\n<h3>League Csv Statement<\/h3>\n<p>For that reason I always prefer using the <code>League\\Csv\\Statement<\/code> class. The class uses closures and callables to filter the CSV document as shown below:<\/p>\n<script src=\"https:\/\/gist.github.com\/12e6d01da00176ce00a70f9288561bc9.js?file=csv-filtering.php\"><\/script>\n<h3>Doctrine Collection Criteria<\/h3>\n<p>While using the <code>League\\Csv\\Statement<\/code> is straightforward, building the correct closure\/callable for a given filtering can become a daunting task. Luckily for us, the people behind the Doctrine project have long since resolve this issue with their powerful expression API by introducing the <code>Doctrine\\Common\\Collections\\Criteria<\/code> class.<\/p>\n<p>Using the <code>Criteria<\/code> class you can rewrite my previous example as follow:<\/p>\n<script src=\"https:\/\/gist.github.com\/12e6d01da00176ce00a70f9288561bc9.js?file=criteria-example.php\"><\/script>\n<p>Which may sound a bit overcomplicated for a simple query but ease debugging and maintenance as filtering requirements become more complex. <a href=\"https:\/\/www.doctrine-project.org\/projects\/doctrine-collections\/en\/latest\/expressions.html#expressions\">The expression API<\/a> is similar to SQL statements so they are easy to understand and use.<\/p>\n<h3>From Doctrine Criteria to Csv Statement<\/h3>\n<p>This package enables converting any <code>Doctrine\\Common\\Collections\\Criteria<\/code> object into a <code>League\\Csv\\Statement<\/code> object allowing using Doctrine Expression API on any CSV document.<\/p>\n<script src=\"https:\/\/gist.github.com\/12e6d01da00176ce00a70f9288561bc9.js?file=criteria-converted.php\"><\/script>\n<h3>From&nbsp; a Csv Reader to a Doctrine Collection<\/h3>\n<p>But that&#8217;s not all the package also comes bundle with an implementation of Doctrine Collection, the <code>Bakame\\Csv\\Extension\\RecordCollection<\/code> that can turn any <code>League\\Csv\\Reader<\/code> or <code>League\\Csv\\ResultSet<\/code> into a Doctrine Collection.<\/p>\n<script src=\"https:\/\/gist.github.com\/12e6d01da00176ce00a70f9288561bc9.js?file=record-collection.php\"><\/script>\n<h3>Final notes<\/h3>\n<p>Depending on your CSV size you may prefer using the <code>Bakame\\Csv\\Extension\\RecordCollection<\/code> class instead of the <code>League\\Csv\\ResultSet<\/code> as the latter uses PHP Iterator and streaming capabilities to reduce memory usage.<\/p>\n<h3>Last but not least<\/h3>\n<p>The <a href=\"https:\/\/github.com\/bakame-php\/csv-doctrine-collections-bridge\">League CSV &#8211; Doctrine Collection Bridge<\/a> is an open source project with a MIT License so contributions are more than welcome and will be fully credited. These contributions can be anything from reporting an issue, requesting or adding missing features or simply improving or correcting some typo on the documentation website.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Attention: Les informations de ce billet sont susceptibles d&#8217;&ecirc;tre obsol&egrave;tes car vieux de plus 2 ans. Warning: The information you are reading may be obsolete, this post was published more than 2 years ago. I&#8217;m happy to announce the release of League CSV &#8211; Doctrine Collection Bridge&nbsp;version 1.0.0. The new package under the bakame namespace [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[43,816,761,815,765,817,412,818,773],"class_list":["post-2732","post","type-post","status-publish","format-standard","hentry","category-web","tag-array","tag-collections","tag-csv","tag-doctrine","tag-filtering","tag-iterator","tag-php","tag-query","tag-stream"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/comments?post=2732"}],"version-history":[{"count":4,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2732\/revisions"}],"predecessor-version":[{"id":2745,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2732\/revisions\/2745"}],"wp:attachment":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/media?parent=2732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/categories?post=2732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/tags?post=2732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}