1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-22 18:02:25 +01:00
php-dibi/examples/using-fluent-syntax.php

78 lines
1.7 KiB
PHP
Raw Normal View History

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
date_default_timezone_set('Europe/Prague');
2008-07-17 03:51:29 +00:00
dibi::connect(array(
2015-06-19 03:11:36 +02:00
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
2008-07-17 03:51:29 +00:00
));
$id = 10;
$record = array(
2015-06-19 03:11:36 +02:00
'title' => 'Super product',
'price' => 318,
2008-07-17 03:51:29 +00:00
'active' => TRUE,
);
// 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]
// 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)
->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')
->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')
->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]