Пример #1
0
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// creates a new record (INSERT)
// The following code inserts a record into table0, but also into the
// tables table1, table2 and table3. Finally, the fields table1_id, table2_id and table3_id
// are updated with the primary keys of the previous tables.
echo "### Creates a new record\n";
$r = new DbRecord($db, "table0");
$r->save(["title" => "Title", "created_at" => date("Y-m-d H:i:s"), "table1.title" => "Title 1", "table2.title" => "Title 2", "table3.title" => "Title 3"]);
// fetches column values (SELECT)
// The following codes fetches the column values of 'table0', but also of the
// tables 'table1', 'table2' and 'table3'.
echo "\n### Fetches column values\n";
list($id, $title, $createdAt, $t1Title, $t2Title, $t3Title) = $r->fetch(["id", "title", "created_at", "table1.title", "table2.title", "table3.title"]);
echo "id: {$id}, title: {$title}, created_at: {$createdAt}, ";
echo "table1.title: {$t1Title}, table2.title: {$t2Title}, table3.title: {$t3Title}\n";
// selects a record and prints its column values (SELECT)
// The following code fetches column values from table0, but also from table1
echo "\n### Selects a record and prints its column values\n";
$r = new DbRecord($db, "table0", $id);
list($title, $createdAt, $t1Title, $t2Title, $t3Title) = $r->fetch(["title", "created_at", "table1.title", "table2.title", "table3.title"]);
echo "title: {$title}, created_at: {$createdAt}, ";
echo "table1.title: {$t1Title}, table2.title: {$t2Title}, table3.title: {$t3Title}\n";
// deletes the previous record (DELETE)
// The following code deletes the previous created record, but also
// the linked records (table1, table2 and table3)
echo "\n### Deletes the previous record and its linked records (table1, table2 and table3)\n";
$r->delete();
Пример #2
0
/**
 * This PHP script illustrates how to operate over a single table (INSERT, SELECT and DELETE).
 * 
 * For more complex examples see test2.php.
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecord;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// creates a new record (INSERT)
echo "### Creates a new record\n";
$r = new DbRecord($db, "table0");
$r->save(["title" => "Title", "created_at" => date("Y-m-d H:i:s")]);
// fetches column values (SELECT)
echo "\n### Fetches column values\n";
list($id, $title, $createdAt) = $r->fetch(["id", "title", "created_at"]);
echo "id: {$id}, title: {$title}, created_at: {$createdAt}\n";
// selects a record and prints its column values (SELECT)
echo "\n### Selects a record and prints its column values\n";
$r = new DbRecord($db, "table0", $id);
list($title, $createdAt) = $r->fetch(["title", "created_at"]);
echo "title: {$title}, created_at: {$createdAt}\n";
// deletes the previous record (DELETE)
echo "\n### Deletes the previous record\n";
$r->delete();
Пример #3
0
 * In this example table3 is linked to table2 which is linked to table1 which is linked to table0.
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecord;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// 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";
$r = new DbRecord($db, "table0");
$r->save(["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) = $r->fetch(["table1[id = table1_id].title", "table2[id = table1[id = table1_id].table2_id].title", "table3[id = table2[id = table1.table2_id].table3_id].title"]);
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) = $r->fetch(["table1.title", "table2[table1.table2_id].title", "table3[table2[table1.table2_id].table3_id].title"]);
echo "table1.title: {$table1Title}, table2.title: {$table2Title}, table3.title: {$table3Title}\n";
Пример #4
0
 * A 'column path' is a way to access columns from linked tables (left joined tables).
 * 
 * For more complex examples see test4.php.
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecord;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// First of all, let's create a new record
// The following code creates a new record and also a record for table1 (left joined table)
echo "### Creates a new record\n";
$r = new DbRecord($db, "table0");
$r->save(["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) = $r->fetch(["table1[id = table1_id].title"]);
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) = $r->fetch(["table1.title"]);
echo "table1.title: {$table1Title}\n";
Пример #5
0
 /**
  * Registers a list of columns.
  * 
  * @param DbRecord $record   Record
  * @param string[] $colPaths Column paths
  * 
  * @return DbRecordColumn[]
  */
 private function _regColumns($record, $colPaths)
 {
     $ret = [];
     foreach ($colPaths as $colPath) {
         array_push($ret, $record->regColumn($colPath));
     }
     return $ret;
 }