欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php NotORM(PHP的ORM框架)示例代码

程序员文章站 2023-12-22 22:30:58
...

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. ?>
复制代码
NotORM, php, ORM

上一篇:

下一篇: