php NotORM(PHP的ORM架構)範例程式碼

來源:互聯網
上載者:User

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
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.