Exemplo n.º 1
0
 * 2. Import test/test.sql to create necessary tables
 * 3. Change config.php with correct values (database, username and password)
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecordTable;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// table manager
$t = new DbRecordTable($db, "table0");
// First of all, let's create a new record
// the following code operates over three tables: table0, table1 and table2
// table1 is linked to table0 (table2[id = table1.table2_id])
// and table2 is linked to the table1 (table2[id = table1.table2_id])
echo "### Inserts a record\n";
$id = $t->insert(["title" => "Title", "created_at" => date("Y-m-d H:i:s"), "table1.title" => "Title 1", "table2[table1.table2_id].title" => "Title 12"]);
// And now prints table2[table1.table2_id].title
// table2 is linked to table1 by 'table2[id = table1.table2_id]'
// table1 is linked to table0 by 'table0[id = table1_id]'
echo "\n### General example: table2[id = table1.table2_id].title\n";
list($table2Title) = $t->select(["table2[id = table1.table2_id].title"], $id);
echo "table2.title: {$table2Title}\n";
// AS THE PREVIOUS EXAMPLE IS VERY COMMON, it can be written as follows:
// note that id and <table>_id can be ommmited
echo "\n### Shorthand example: table2[table1.table2_id].title\n";
list($table2Title) = $t->select(["table2[table1.table2_id].title"], $id);
echo "table2.title: {$table2Title}\n";
Exemplo n.º 2
0
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecordTable;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// table manager
$t = new DbRecordTable($db, "table0");
// First of all, let's create a new record
// the following code operates over four tables: table0, table1, table2 and table3
// table1 is linked to table0 (table2[id = table1.table2_id])
// and table2 is linked to table1 (table2[id = table1.table2_id])
// and table3 is linked to table2 (table3[id = table2.table3_id])
echo "### Creates a new record\n";
$id = $t->insert(["title" => "Title", "created_at" => date("Y-m-d H:i:s"), "table1.title" => "Title 1", "table2[table1.table2_id].title" => "Title 12", "table3[table2[table1.table2_id].table3_id].title" => "Title 123"]);
// And now prints table titles
// table3 is linked to table2 by 'table3[id = table2.table3_id]'
// table2 is linked to table1 by 'table2[id = table1.table2_id]'
// table1 is linked to table0 by 'table0[id = table1_id]'
echo "\n### General example\n";
list($table1Title, $table2Title, $table3Title) = $t->select(["table1[id = table1_id].title", "table2[id = table1[id = table1_id].table2_id].title", "table3[id = table2[id = table1.table2_id].table3_id].title"], $id);
echo "table1.title: {$table1Title}, table2.title: {$table2Title}, table3.title: {$table3Title}\n";
// AS THE PREVIOUS EXAMPLE IS VERY COMMON, it can be written as follows:
// note that id and <table>_id can be ommited
echo "\n### Shorthand example\n";
list($table1Title, $table2Title, $table3Title) = $t->select(["table1.title", "table2[table1.table2_id].title", "table3[table2[table1.table2_id].table3_id].title"], $id);
echo "table1.title: {$table1Title}, table2.title: {$table2Title}, table3.title: {$table3Title}\n";
Exemplo n.º 3
0
 * 1. Create a new database (for example 'test')
 * 2. Import test/test.sql to create necessary tables
 * 3. Change config.php with correct values (database, username and password)
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecordTable;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// table manager
$t = new DbRecordTable($db, "table0");
// inserts records into multiple tables
echo "### Inserts a record\n";
$id = $t->insert(["title" => "Title", "created_at" => date("Y-m-d H:i:s"), "table1.title" => "Title 1"]);
// And now prints a 'left joined' or 'linked' table column
// table1 is the 'left joined' or 'linked' table
// id is a column of table1 (not necessarily the primary key)
// table1_id is a column of table0
// title is a column of table1 (the column to print)
echo "\n### General example: table1[id = table1_id].title\n";
list($table1Title) = $t->select(["table1[id = table1_id].title"], $id);
echo "table1.title: {$table1Title}\n";
// AS THE PREVIOUS EXAMPLE IS VERY COMMON, it can be written as follows:
// note that 'id' and 'table1_id' have been omitted
echo "\n### Shorthand example: table1.title\n";
list($table1Title) = $t->select(["table1.title"], $id);
echo "table1.title: {$table1Title}\n";
Exemplo n.º 4
0
 * table1_id, table2_id and table3_id.
 * 
 * How to test this script:
 * 1. Create a new database (for example 'test')
 * 2. Import test/test.sql to create necessary tables
 * 3. Change config.php with correct values (database, username and password)
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecordTable;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// table manager
$t = new DbRecordTable($db, "table0");
// inserts records into multiple tables
// the following code inserts a record into table0 and, eventually, into table1, table2 and table3
echo "### Inserts records into multiple tables\n";
$id = $t->insert(["title" => "Title", "created_at" => date("Y-m-d H:i:s"), "table1.title" => "Title 1", "table2.title" => "Title 2", "table3.title" => "Title 3"]);
// selects records from multiple tables
// the following code selects a record from table0 and, eventually, from table1, table2 and table3
echo "\n### Selects records from multiple tables\n";
list($title, $createdAt, $t1Title, $t2Title, $t3Title) = $t->select(["title", "created_at", "table1.title", "table2.title", "table3.title"], $id);
echo "id: {$id}, title: {$title}, created_at: {$createdAt}, ";
echo "table1.title: {$t1Title}, table2.title: {$t2Title}, table3.title: {$t3Title}\n";
// deletes the previous record
echo "\n### Deletes the previous record\n";
$t->delete($id);
Exemplo n.º 5
0
 * This PHP script illustrates how to operate over a single table (INSERT, SELECT and DELETE).
 * 
 * For more complex examples see test2.php.
 * 
 * How to test this script:
 * 1. Create a new database (for example 'test')
 * 2. Import test/test.sql to create necessary tables
 * 3. Change config.php with correct values (database, username and password)
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
require_once "config.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecordTable;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// table manager
$t = new DbRecordTable($db, "table0");
// inserts a record
echo "### Inserts a record\n";
$id = $t->insert(["title" => "Title", "created_at" => date("Y-m-d H:i:s")]);
// selects a record
echo "\n### Selects a record\n";
list($title, $createdAt) = $t->select(["title", "created_at"], $id);
echo "id: {$id}, title: {$title}, created_at: {$createdAt}\n";
// deletes a record
echo "\n### Deletes the previous record\n";
$t->delete($id);