A lap around PHPLinqIntroducing PHP Language Integrated QueryMaarten Balliauwhttp://blog.maartenballiauw.behttp://twitter.com/maartenballiauwmaarten@maartenballiauw.be
Who am I?Maarten BalliauwAntwerp, Belgiumwww.realdolmen.comFocus on webASP.NET, ASP.NET MVC, PHP, Azure, VSTS, …MVP ASP.NEThttp://blog.maartenballiauw.behttp://twitter.com/maartenballiauw
Agenda LINQ?
 PHPLINQ?
 Demo
 Q&ALINQLanguageINtegrated Query.NET world (.NET 3.5)1 query languageMultiple providersLanguageextensionsAnonymous typesObject initializerLambdaexpressions
LINQ – Example…int[] someNumbers = new int[] { 1, 2, 3, 4, 5 };var results = from x in someNumberswhere x < 3            select new { Number = x };foreach (var result in results) {Console.WriteLine(result.Number);}
PHPLinqPHP LanguageIntegrated QueryPHP userlandlibrary1 query languageMultiple providersFeaturingAnonymous typesObject initializerLambdaexpressions
PHPLinq – Example…$someNumbers = newarray(1, 2, 3, 4, 5);$results = from('$x')->in($someNumbers)         ->where('$x => $x < 3')         ->select('new { "Number" => $x }');foreach ($results as $result) {    print($result->Number);}
PHPLinq providers
LinqToObjects$employees = array(    new Employee(1, 1, 5, 'Maarten', 'maarten@example.com', 24),    new Employee(2, 1, 5, 'Paul', 'paul@example.com', 30),    new Employee(3, 2, 5, 'Bill', 'bill.a@example.com', 29),    new Employee(4, 3, 5, 'Bill', 'bill.g@example.com', 28),    new Employee(5, 2, 0, 'Xavier', 'xavier@example.com', 40));$result = from('$employee')->in($employees)       ->where('$employee => substr($employee->Name, 0, 4) == "Bill"')       ->select('new {               "Name" => $employee->Name,               "Email" => $employee->Email             }');Array(    [0] => stdClass Object        (            [Name] => Bill            [Email] => bill.a@example.com        )    [1] => stdClass Object        (            [Name] => Bill            [Email] => bill.g@example.com        ))
LinqToZendDbclass EmployeeTable extends Zend_Db_Table {    protected $_name = 'employees'; // table name    protected $_primary = 'Id';}$employeeTable = new EmployeeTable(array('db' => $db));$employeeTable->setRowClass('Employee');$result = from('$employee')->in($employeeTable)       ->where('$employee => substr($employee->Name, 0, 4) == "Bill"')       ->select('new {               "Name" => $employee->Name,               "Email" => $employee->Email             }');Array(    [0] => stdClass Object        (            [Name] => Bill            [Email] => bill.a@example.com        )    [1] => stdClass Object        (            [Name] => Bill            [Email] => bill.g@example.com        ))
LinqToZendDb – Provider model$result = from('$employee')->in($employeeTable)            ->where('$employee => trim($employee->Name) == "Bill"')            ->select('$employee->Name');Query on SQLiteSELECT "$employee".* FROM "employees" AS "$employee"WHERE (TRIM('$employee'."Name")  =  "Bill")Query on Microsoft SQL ServerSELECT "$employee".* FROM "employees" AS "$employee"WHERE (LTRIM(RTRIM('$employee'."Name"))  =  "Bill")
LinqToAzure$result = from('$employee')->in( array($azCli, 'employees', 'Employee') )       ->where('$employee => $employee->Name == "Bill"')       ->select('new {               "Name" => $employee->Name,               "Email" => $employee->Email             }');Array(    [0] => stdClass Object        (            [Name] => Bill            [Email] => bill.a@example.com        )    [1] => stdClass Object        (            [Name] => Bill            [Email] => bill.g@example.com        ))

PHPLinq

  • 1.
    A lap aroundPHPLinqIntroducing PHP Language Integrated QueryMaarten Balliauwhttp://blog.maartenballiauw.behttp://twitter.com/maartenballiauwmaarten@maartenballiauw.be
  • 2.
    Who am I?MaartenBalliauwAntwerp, Belgiumwww.realdolmen.comFocus on webASP.NET, ASP.NET MVC, PHP, Azure, VSTS, …MVP ASP.NEThttp://blog.maartenballiauw.behttp://twitter.com/maartenballiauw
  • 3.
  • 4.
  • 5.
  • 6.
    Q&ALINQLanguageINtegrated Query.NETworld (.NET 3.5)1 query languageMultiple providersLanguageextensionsAnonymous typesObject initializerLambdaexpressions
  • 7.
    LINQ – Example…int[]someNumbers = new int[] { 1, 2, 3, 4, 5 };var results = from x in someNumberswhere x < 3 select new { Number = x };foreach (var result in results) {Console.WriteLine(result.Number);}
  • 8.
    PHPLinqPHP LanguageIntegrated QueryPHPuserlandlibrary1 query languageMultiple providersFeaturingAnonymous typesObject initializerLambdaexpressions
  • 9.
    PHPLinq – Example…$someNumbers= newarray(1, 2, 3, 4, 5);$results = from('$x')->in($someNumbers) ->where('$x => $x < 3') ->select('new { "Number" => $x }');foreach ($results as $result) { print($result->Number);}
  • 10.
  • 11.
    LinqToObjects$employees = array(   new Employee(1, 1, 5, 'Maarten', 'maarten@example.com', 24),    new Employee(2, 1, 5, 'Paul', 'paul@example.com', 30),    new Employee(3, 2, 5, 'Bill', 'bill.a@example.com', 29),    new Employee(4, 3, 5, 'Bill', 'bill.g@example.com', 28),    new Employee(5, 2, 0, 'Xavier', 'xavier@example.com', 40));$result = from('$employee')->in($employees)       ->where('$employee => substr($employee->Name, 0, 4) == "Bill"')       ->select('new {               "Name" => $employee->Name,               "Email" => $employee->Email             }');Array(    [0] => stdClass Object        (            [Name] => Bill            [Email] => bill.a@example.com        )    [1] => stdClass Object        (            [Name] => Bill            [Email] => bill.g@example.com        ))
  • 12.
    LinqToZendDbclass EmployeeTable extendsZend_Db_Table {    protected $_name = 'employees'; // table name    protected $_primary = 'Id';}$employeeTable = new EmployeeTable(array('db' => $db));$employeeTable->setRowClass('Employee');$result = from('$employee')->in($employeeTable)       ->where('$employee => substr($employee->Name, 0, 4) == "Bill"')       ->select('new {               "Name" => $employee->Name,               "Email" => $employee->Email             }');Array(    [0] => stdClass Object        (            [Name] => Bill            [Email] => bill.a@example.com        )    [1] => stdClass Object        (            [Name] => Bill            [Email] => bill.g@example.com        ))
  • 13.
    LinqToZendDb – Providermodel$result = from('$employee')->in($employeeTable)            ->where('$employee => trim($employee->Name) == "Bill"')            ->select('$employee->Name');Query on SQLiteSELECT "$employee".* FROM "employees" AS "$employee"WHERE (TRIM('$employee'."Name")  =  "Bill")Query on Microsoft SQL ServerSELECT "$employee".* FROM "employees" AS "$employee"WHERE (LTRIM(RTRIM('$employee'."Name"))  =  "Bill")
  • 14.
    LinqToAzure$result = from('$employee')->in(array($azCli, 'employees', 'Employee') )       ->where('$employee => $employee->Name == "Bill"')       ->select('new {               "Name" => $employee->Name,               "Email" => $employee->Email             }');Array(    [0] => stdClass Object        (            [Name] => Bill            [Email] => bill.a@example.com        )    [1] => stdClass Object        (            [Name] => Bill            [Email] => bill.g@example.com        ))