Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHPLinq

1,652 views

Published on

MSWDS Session on PHPLinq - www.phplinq.net

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

PHPLinq

  1. 1. A lap around PHPLinq<br />Introducing PHP Language Integrated Query<br />Maarten Balliauw<br />http://blog.maartenballiauw.be<br />http://twitter.com/maartenballiauw<br />maarten@maartenballiauw.be<br />
  2. 2. Who am I?<br />Maarten Balliauw<br />Antwerp, Belgium<br />www.realdolmen.com<br />Focus on web<br />ASP.NET, ASP.NET MVC, PHP, Azure, VSTS, …<br />MVP ASP.NET<br />http://blog.maartenballiauw.be<br />http://twitter.com/maartenballiauw<br />
  3. 3. Agenda<br /><ul><li> LINQ?
  4. 4. PHPLINQ?
  5. 5. Demo
  6. 6. Q&A</li></li></ul><li>LINQ<br />LanguageINtegrated Query<br />.NET world (.NET 3.5)<br />1 query language<br />Multiple providers<br />Languageextensions<br />Anonymous types<br />Object initializer<br />Lambdaexpressions<br />
  7. 7. LINQ – Example…<br />int[] someNumbers = new int[] { 1, 2, 3, 4, 5 };<br />var results = from x in someNumberswhere x &lt; 3 select new { Number = x };<br />foreach (var result in results) {<br />Console.WriteLine(result.Number);<br />}<br />
  8. 8. PHPLinq<br />PHP LanguageIntegrated Query<br />PHP userlandlibrary<br />1 query language<br />Multiple providers<br />Featuring<br />Anonymous types<br />Object initializer<br />Lambdaexpressions<br />
  9. 9. PHPLinq – Example…<br />$someNumbers = newarray(1, 2, 3, 4, 5);<br />$results = from(&apos;$x&apos;)-&gt;in($someNumbers) -&gt;where(&apos;$x =&gt; $x &lt; 3&apos;) -&gt;select(&apos;new { &quot;Number&quot; =&gt; $x }&apos;);<br />foreach ($results as $result) {<br /> print($result-&gt;Number);<br />}<br />
  10. 10. PHPLinq providers<br />
  11. 11. LinqToObjects<br />$employees = array(    new Employee(1, 1, 5, &apos;Maarten&apos;, &apos;maarten@example.com&apos;, 24),    new Employee(2, 1, 5, &apos;Paul&apos;, &apos;paul@example.com&apos;, 30),    new Employee(3, 2, 5, &apos;Bill&apos;, &apos;bill.a@example.com&apos;, 29),    new Employee(4, 3, 5, &apos;Bill&apos;, &apos;bill.g@example.com&apos;, 28),    new Employee(5, 2, 0, &apos;Xavier&apos;, &apos;xavier@example.com&apos;, 40)<br />);<br />$result = from(&apos;$employee&apos;)-&gt;in($employees)       -&gt;where(&apos;$employee =&gt; substr($employee-&gt;Name, 0, 4) == &quot;Bill&quot;&apos;)       -&gt;select(&apos;new {               &quot;Name&quot; =&gt; $employee-&gt;Name,               &quot;Email&quot; =&gt; $employee-&gt;Email             }&apos;);<br />Array(    [0] =&gt; stdClass Object        (            [Name] =&gt; Bill            [Email] =&gt; bill.a@example.com        )    [1] =&gt; stdClass Object        (            [Name] =&gt; Bill            [Email] =&gt; bill.g@example.com        ))<br />
  12. 12. LinqToZendDb<br />class EmployeeTable extends Zend_Db_Table {    protected $_name = &apos;employees&apos;; // table name    protected $_primary = &apos;Id&apos;;<br />}<br />$employeeTable = new EmployeeTable(array(&apos;db&apos; =&gt; $db));<br />$employeeTable-&gt;setRowClass(&apos;Employee&apos;);<br />$result = from(&apos;$employee&apos;)-&gt;in($employeeTable)       -&gt;where(&apos;$employee =&gt; substr($employee-&gt;Name, 0, 4) == &quot;Bill&quot;&apos;)       -&gt;select(&apos;new {               &quot;Name&quot; =&gt; $employee-&gt;Name,               &quot;Email&quot; =&gt; $employee-&gt;Email             }&apos;);<br />Array(    [0] =&gt; stdClass Object        (            [Name] =&gt; Bill            [Email] =&gt; bill.a@example.com        )    [1] =&gt; stdClass Object        (            [Name] =&gt; Bill            [Email] =&gt; bill.g@example.com        ))<br />
  13. 13. LinqToZendDb – Provider model<br />$result = from(&apos;$employee&apos;)-&gt;in($employeeTable)            -&gt;where(&apos;$employee =&gt; trim($employee-&gt;Name) == &quot;Bill&quot;&apos;)            -&gt;select(&apos;$employee-&gt;Name&apos;);<br />Query on SQLiteSELECT &quot;$employee&quot;.* FROM &quot;employees&quot; AS &quot;$employee&quot;WHERE (TRIM(&apos;$employee&apos;.&quot;Name&quot;)  =  &quot;Bill&quot;)<br />Query on Microsoft SQL ServerSELECT &quot;$employee&quot;.* FROM &quot;employees&quot; AS &quot;$employee&quot;WHERE (LTRIM(RTRIM(&apos;$employee&apos;.&quot;Name&quot;))  =  &quot;Bill&quot;)<br />
  14. 14. LinqToAzure<br />$result = from(&apos;$employee&apos;)-&gt;in( array($azCli, &apos;employees&apos;, &apos;Employee&apos;) )       -&gt;where(&apos;$employee =&gt; $employee-&gt;Name == &quot;Bill&quot;&apos;)       -&gt;select(&apos;new {               &quot;Name&quot; =&gt; $employee-&gt;Name,               &quot;Email&quot; =&gt; $employee-&gt;Email             }&apos;);<br />Array(    [0] =&gt; stdClass Object        (            [Name] =&gt; Bill            [Email] =&gt; bill.a@example.com        )    [1] =&gt; stdClass Object        (            [Name] =&gt; Bill            [Email] =&gt; bill.g@example.com        ))<br />
  15. 15. Parsing XML?<br />$rssFeed = simplexml_load_string(file_get_contents(&apos;http://blog.maartenballiauw.be/syndication.axd&apos;)); <br />$result = from(&apos;$item&apos;)-&gt;in($rssFeed-&gt;xpath(&apos;//channel/item&apos;))        -&gt;orderByDescending(&apos;$item =&gt; strtotime((string)$item-&gt;pubDate)&apos;)        -&gt;take(2)        -&gt;select(&apos;new {                        &quot;Title&quot; =&gt; (string)$item-&gt;title,                        &quot;Author&quot; =&gt; (string)$item-&gt;author                 }&apos;);<br />
  16. 16. Joining multiple datasources?<br />Notofficiallysupported!<br />
  17. 17. Questions?<br />Check http://phplinq.codeplex.com<br />Contact me:<br />http://blog.maartenballiauw.be<br />maarten@maartenballiauw.be<br />http://twitter.com/maartenballiauw<br />Thankyou!<br />

×