当前位置:Gxlcms > PHP教程 > phpNotORM(PHP的ORM框架)示例代码

phpNotORM(PHP的ORM框架)示例代码

时间:2021-07-01 10:21:17 帮助过:32人阅读

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

人气教程排行