2017-07-21 21:34:37 +02:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
?>
|
2010-08-03 12:28:07 +02:00
|
|
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
|
|
|
|
2018-05-10 21:45:11 +02:00
|
|
|
<h1>Using Fluent Syntax | Dibi</h1>
|
2010-08-03 12:28:07 +02:00
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
<?php
|
|
|
|
|
2018-03-23 11:57:59 +01:00
|
|
|
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
|
|
|
die('Install packages using `composer install`');
|
|
|
|
}
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2009-04-16 02:15:20 +00:00
|
|
|
date_default_timezone_set('Europe/Prague');
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2018-04-17 10:03:43 +02:00
|
|
|
$dibi = new Dibi\Connection([
|
2018-05-23 17:08:25 +02:00
|
|
|
'driver' => 'sqlite',
|
2012-10-18 23:00:12 +02:00
|
|
|
'database' => 'data/sample.s3db',
|
2015-10-06 01:39:01 +02:00
|
|
|
]);
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
$id = 10;
|
2015-10-06 01:39:01 +02:00
|
|
|
$record = [
|
2015-06-19 03:11:36 +02:00
|
|
|
'title' => 'Super product',
|
|
|
|
'price' => 318,
|
2017-07-11 12:28:13 +02:00
|
|
|
'active' => true,
|
2015-10-06 01:39:01 +02:00
|
|
|
];
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
// SELECT ...
|
2018-04-17 10:03:43 +02:00
|
|
|
$dibi->select('product_id')->as('id')
|
2008-07-17 03:51:29 +00:00
|
|
|
->select('title')
|
|
|
|
->from('products')
|
|
|
|
->innerJoin('orders')->using('(product_id)')
|
|
|
|
->innerJoin('customers USING (customer_id)')
|
|
|
|
->orderBy('title')
|
|
|
|
->test();
|
2008-10-28 02:06:55 +00:00
|
|
|
// -> SELECT [product_id] AS [id] , [title] FROM [products] INNER JOIN [orders]
|
|
|
|
// USING (product_id) INNER JOIN customers USING (customer_id) ORDER BY [title]
|
|
|
|
|
|
|
|
|
2008-09-13 16:38:59 +00:00
|
|
|
// SELECT ...
|
2018-04-17 10:03:43 +02:00
|
|
|
echo $dibi->select('title')->as('id')
|
2008-09-13 16:38:59 +00:00
|
|
|
->from('products')
|
|
|
|
->fetchSingle();
|
2008-10-28 02:06:55 +00:00
|
|
|
// -> Chair (as result of query: SELECT [title] AS [id] FROM [products])
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
// INSERT ...
|
2018-04-17 10:03:43 +02:00
|
|
|
$dibi->insert('products', $record)
|
2008-07-17 03:51:29 +00:00
|
|
|
->setFlag('IGNORE')
|
|
|
|
->test();
|
2008-10-28 02:06:55 +00:00
|
|
|
// -> INSERT IGNORE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
// UPDATE ...
|
2018-04-17 10:03:43 +02:00
|
|
|
$dibi->update('products', $record)
|
2012-01-03 05:14:44 +01:00
|
|
|
->where('product_id = ?', $id)
|
2008-07-17 03:51:29 +00:00
|
|
|
->test();
|
2008-10-28 02:06:55 +00:00
|
|
|
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
// DELETE ...
|
2018-04-17 10:03:43 +02:00
|
|
|
$dibi->delete('products')
|
2012-01-03 05:14:44 +01:00
|
|
|
->where('product_id = ?', $id)
|
2008-07-17 03:51:29 +00:00
|
|
|
->test();
|
2008-10-28 02:06:55 +00:00
|
|
|
// -> DELETE FROM [products] WHERE product_id = 10
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
// custom commands
|
2018-04-17 10:03:43 +02:00
|
|
|
$dibi->command()
|
2008-07-17 03:51:29 +00:00
|
|
|
->update('products')
|
2012-01-03 05:14:44 +01:00
|
|
|
->where('product_id = ?', $id)
|
2008-07-17 03:51:29 +00:00
|
|
|
->set($record)
|
|
|
|
->test();
|
2008-10-28 02:06:55 +00:00
|
|
|
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
|
|
|
|
|
|
|
|
|
2018-04-17 10:03:43 +02:00
|
|
|
$dibi->command()
|
2008-07-17 03:51:29 +00:00
|
|
|
->truncate('products')
|
|
|
|
->test();
|
2008-10-28 02:06:55 +00:00
|
|
|
// -> TRUNCATE [products]
|