using-fluent-syntax.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
  2. <h1>Using Fluent Syntax | dibi</h1>
  3. <?php
  4. require __DIR__ . '/../src/loader.php';
  5. date_default_timezone_set('Europe/Prague');
  6. dibi::connect([
  7. 'driver' => 'sqlite3',
  8. 'database' => 'data/sample.s3db',
  9. ]);
  10. $id = 10;
  11. $record = [
  12. 'title' => 'Super product',
  13. 'price' => 318,
  14. 'active' => true,
  15. ];
  16. // SELECT ...
  17. dibi::select('product_id')->as('id')
  18. ->select('title')
  19. ->from('products')
  20. ->innerJoin('orders')->using('(product_id)')
  21. ->innerJoin('customers USING (customer_id)')
  22. ->orderBy('title')
  23. ->test();
  24. // -> SELECT [product_id] AS [id] , [title] FROM [products] INNER JOIN [orders]
  25. // USING (product_id) INNER JOIN customers USING (customer_id) ORDER BY [title]
  26. // SELECT ...
  27. echo dibi::select('title')->as('id')
  28. ->from('products')
  29. ->fetchSingle();
  30. // -> Chair (as result of query: SELECT [title] AS [id] FROM [products])
  31. // INSERT ...
  32. dibi::insert('products', $record)
  33. ->setFlag('IGNORE')
  34. ->test();
  35. // -> INSERT IGNORE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
  36. // UPDATE ...
  37. dibi::update('products', $record)
  38. ->where('product_id = ?', $id)
  39. ->test();
  40. // -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
  41. // DELETE ...
  42. dibi::delete('products')
  43. ->where('product_id = ?', $id)
  44. ->test();
  45. // -> DELETE FROM [products] WHERE product_id = 10
  46. // custom commands
  47. dibi::command()
  48. ->update('products')
  49. ->where('product_id = ?', $id)
  50. ->set($record)
  51. ->test();
  52. // -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
  53. dibi::command()
  54. ->truncate('products')
  55. ->test();
  56. // -> TRUNCATE [products]