1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-23 10:26:21 +01:00
php-dibi/examples/using-fluent-syntax.php

83 lines
1.8 KiB
PHP
Raw Normal View History

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
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
2008-07-17 03:51:29 +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([
'driver' => 'sqlite',
'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,
'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]
// SELECT ...
2018-04-17 10:03:43 +02:00
echo $dibi->select('title')->as('id')
->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)
->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')
->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')
->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]