在烈火下載欄目,我們發布了NotORM,這是一個完全免費的 PHP 庫,用來簡化和數據庫的交互。最有特色的功能是處理表關聯關系非常簡單。另外 NotORM 的性能非常的高,設置高過內置的驅動。
下邊來看一些NotORM的示例代碼:
連接數據庫:
Copy to Clipboard引用的內容:[www.bkjia.com]
include "NotORM.php";
$pdo = new PDO("mysql:dbname=software");
$db = new NotORM($pdo);
讀取數據:
Copy to Clipboard引用的內容:[www.bkjia.com]
foreach ($db->application() as $application) { // get all applications
echo "$application[title]\n"; // print application title
}
條件查詢:
Copy to Clipboard引用的內容:[www.bkjia.com]
$applications = $db->application()
->select("id, title")
->where("web LIKE ?", "http://%")
->order("title")
->limit(10)
;
foreach ($applications as $id => $application) {
echo "$application[title]\n";
}
讀取結果:
Copy to Clipboard引用的內容:[www.bkjia.com]
$application = $db->application[1]; // get by primary key
$application = $db->application("title = ?", "Adminer")->fetch();
處理表關聯:
Copy to Clipboard引用的內容:[www.bkjia.com]
echo $application->author["name"] . "\n"; // get name of the application author
foreach ($application->application_tag() as $application_tag) { // get all tags of $application
echo $application_tag->tag["name"] . "\n"; // print the tag name
}
JOIN聯合查詢:
Copy to Clipboard引用的內容:[www.bkjia.com]
// get all applications ordered by author's name
foreach ($db->application()->order("author.name") as $application) {
echo $application->author["name"] . ": $application[title]\n";
}
結果集分組:
Copy to Clipboard引用的內容:[www.bkjia.com]
echo $db->application()->max("id"); // get maximum ID
foreach ($db->application() as $application) {
// get count of each application's tags
echo $application->application_tag()->count("*") . "\n";
}
完整實例:
Copy to Clipboard引用的內容:[www.bkjia.com]
<?php
include "NotORM.php";
$connection = new PDO("mysql:dbname=software");
$software = new NotORM($connection);
foreach ($software->application()->order("title") as $application) { // get all applications ordered by title
echo "$application[title]\n"; // print application title
echo $application->author["name"] . "\n"; // print name of the application author
foreach ($application->application_tag() as $application_tag) { // get all tags of $application
echo $application_tag->tag["name"] . "\n"; // print the tag name
}
}
?>