首页 > 后端开发 > php教程 > php NotORM(PHP的ORM框架)示例代码

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

WBOY
发布: 2016-07-25 08:41:58
原创
1256 人浏览过

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


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板