Home  >  Article  >  Backend Development  >  php NotORM (PHP’s ORM framework) sample code

php NotORM (PHP’s ORM framework) sample code

WBOY
WBOYOriginal
2016-07-25 08:41:581238browse

NotORM is a PHP library used to simplify interaction with databases. The most distinctive feature is that it is very simple to handle table relationships. In addition, the performance of NotORM is very high, and the settings are higher than the built-in driver.

Connect to database

  1. include "NotORM.php";
  2. $pdo = new PDO("mysql:dbname=software");
  3. $db = new NotORM($pdo);
Copy code

Read data

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

Conditional query

  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. }
Copy code

Read results

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

Processing table associations

  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. }
Copy code

JOIN joint query

  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. }
Copy code

Result set grouping

  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. }
Copy code

Full example

  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. ?>
Copy code
NotORM, php, ORM


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP file download!Next article:PHP file download!