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

WBOY
Libérer: 2016-07-25 08:41:58
original
1233 Les gens l'ont consulté

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


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal