首頁 > 後端開發 > php教程 > php NotORM(PHP的ORM框架)範例程式碼

php NotORM(PHP的ORM框架)範例程式碼

WBOY
發布: 2016-07-25 08:41:58
原創
1263 人瀏覽過

NotORM 是一个 PHP 库,用来简化和数据库的交互。最有特色的功能是处理表关联关系非常简单。另外 NotORM 的性能非常的高,设置高过内置的驱动。

连接数据库

  1. include "NotORM.php";
  2. $pdo = new PDO("mysql:dbname=software");
  3. $db = new NotORM($pdo);
复制代码

读取数据

  1. foreach ($db->application() as $application) { // get all applications
  2. echo "$application[title]n"; // print application title
  3. }
复制代码

条件查询

  1. $applications = $db->application()
  2. ->select("id, title")
  3. ->where("web LIKE ?", "http://%")
  4. ->order("title")
  5. ->limit(10)
  6. ;
  7. foreach ($applications as $id => $application) {
  8. echo "$application[title]n";
  9. }
复制代码

读取结果

  1. $application = $db->application[1]; // get by primary key
  2. $application = $db->application("title = ?", "Adminer")->fetch();
复制代码

处理表关联

  1. echo $application->author["name"] . "n"; // get name of the application author
  2. foreach ($application->application_tag() as $application_tag) { // get all tags of $application
  3. echo $application_tag->tag["name"] . "n"; // print the tag name
  4. }
复制代码

JOIN联合查询

  1. // get all applications ordered by author's name
  2. foreach ($db->application()->order("author.name") as $application) {
  3. echo $application->author["name"] . ": $application[title]n";
  4. }
复制代码

结果集分组

  1. echo $db->application()->max("id"); // get maximum ID
  2. foreach ($db->application() as $application) {
  3. // get count of each application's tags
  4. echo $application->application_tag()->count("*") . "n";
  5. }
复制代码

完整例子

  1. include "NotORM.php";
  2. $connection = new PDO("mysql:dbname=software");
  3. $software = new NotORM($connection);
  4. foreach ($software->application()->order("title") as $application) { // get all applications ordered by title
  5. echo "$application[title]n"; // print application title
  6. echo $application->author["name"] . "n"; // print name of the application author
  7. foreach ($application->application_tag() as $application_tag) { // get all tags of $application
  8. echo $application_tag->tag["name"] . "n"; // print the tag name
  9. }
  10. }
  11. ?>
复制代码
不是ORM、php、ORM


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板