2010-08-03 12:28:07 +02:00
|
|
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
|
|
|
|
2010-08-03 11:48:51 +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
|
|
|
|
|
2015-01-13 16:27:01 +01:00
|
|
|
require __DIR__ . '/../src/loader.php';
|
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
|
|
|
|
2015-10-06 01:39:01 +02:00
|
|
|
dibi::connect([
|
2015-06-19 03:11:36 +02:00
|
|
|
'driver' => 'sqlite3',
|
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,
|
2008-07-17 03:51:29 +00:00
|
|
|
'active' => TRUE,
|
2015-10-06 01:39:01 +02:00
|
|
|
];
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
// SELECT ...
|
|
|
|
dibi::select('product_id')->as('id')
|
|
|
|
->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 ...
|
|
|
|
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 ...
|
|
|
|
dibi::insert('products', $record)
|
|
|
|
->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 ...
|
|
|
|
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 ...
|
|
|
|
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
|
|
|
|
dibi::command()
|
|
|
|
->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
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
dibi::command()
|
|
|
|
->truncate('products')
|
|
|
|
->test();
|
2008-10-28 02:06:55 +00:00
|
|
|
// -> TRUNCATE [products]
|