簡體   English   中英

PHP PDO在獲取數據時,能否將數據獲取到包含另一個Object的Object中?

[英]PHP PDO when fetching data, can data be fetched into an Object which contains another Object?

出於純粹的好奇心,誰能迅速告訴我是否可行? 我沒有那樣實現,但是回想一下,我想知道是否可以實現。 這是場景:

產品,服務表以及兩者之間的關系表

 Product ID | Name 1 | Product A 2 | Product B Service ID | Name 1 | Service A 2 | Service B 3 | Service C Price ID | Cost | Product ID | Service ID 1 | 1 | 1 | 1 2 | 2 | 1 | 3 3 | 3 | 2 | 2 4 | 4 | 2 | 3 

因此,如果有表數據對象,則將有:

  • 具有屬性的產品類:id,名稱,服務數組。
  • 類服務具有以下屬性:ID,名稱,成本

 //$query = obtain a list of all products with services and prices;
 //$query = obtain a product and all it's associated services and prices; 
 $statement = $pdo->prepare($query);
 $statement -> execute();
 $results = $statement ->fetchAll(PDO::FETCH_CLASS,"Product");

PDO似乎不支持將任何關系數據嵌套到給定對象中。 看一下fetch的文檔。 您可能可以構建將產品的關系加載到Product類本身並手動調用所需的邏輯。

當PDO在__construct()之前注入類屬性的值時,您也許可以在構造函數中獲得一些伏都教,但我不太確定。

無論哪種方式,這通常都是可靠的ORM層可以為您提供的功能之一。 有些 出去 那里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM