- Object modeling 時採用的 schema builder 看似 ER model,但實際使用 SOQL 操作時,卻是以物件導向的觀點。
- SOQL 沒有 JOIN,只能透過 relationship traversal with path expression 完成各式的 implicit JOIN。
- Object 中記錄 relationship 的 field naming,有獨特的 convention,有時使用 __c,有時卻要使用 __r。
- Relationship 的 cardinality 固定為 one-to-many;使用 SOQL traverse relationship 時,從 one side 或 many side 出發會需要截然不同的 SOQL statement patterns。
- 設定 relationships 可能分為兩類:lookup 與 master-detail (a.k.a. MD)。
- 以物件導向的角度,上述分別是 aggregation 與 composition;前者是 whole-part 的關係,後者有一致的生命週期。
- 文件中的 parent 與 child,並非專屬於 master-detail relationship,純粹分別用於 relationship 中的 one side 與 many side。
- Both relationships are defined from the many-to-1 side, namely from a child to a parent. Note that we have utilized a NESTED select to obtain the records of related children from the parent. This kind of expression is very powerful in obtaining related records traversing from the 1 side in a 1-m relationship.
- It is a useful pattern to obtain related information on a parent and all its children via traversing path expressions with relationship fields.
下表整理了 SOQL 針對不同 side 作為起點,如何透過 relationship traversal 達到各式 JOIN 的 patterns:
Outermost FROM Clause | ||
One Side | Many Side | |
Inner JOIN | 在 WHERE clause 中使用 nested SELECT SELECT Name FROM Position_c WHERE Id IN (SELECT Position__c FROM Job_Application__c) |
在 WHERE clause 中直接取用 relationship SELECT Name,Position__r.Name, FROM Job_Application__c WHERE Position__r.Department__c = ‘Sales’ |
Outer JOIN | 在 SELECT clause 中使用 nested SELECT SELECT Name, (SELECT Name FROM Job_Applications__r) FROM Position__c |
在 SELECT clause 中直接取用 relationship SELECT Name, Position__r.Department__c FROM Job_Application__c |
[2] 說明了定義 relationship 時的命名原則:
- 在 parent object 中定義 relationship 時:名稱須 unique to the parent,並且使用複數形的 child object name 命名。
- 在 child object 中定義 relationship 時:名稱須 unique to the child,並且使用單數形的 parent object name 命名。
- 按前述第 (2) 項原則,relationship filed 的命名應使用單數的 parent object name,此處命名為 Mother_of_Child (應可簡化為 Mother)。
- Child Relationship Name 則以 child object 為主,命名為 Daughters。
=> Lookup relationship 中才談 Child Relationship Name,master-detail relationship 無此欄位。 - 從 child object 為起點實作 outer JOIN:SELECT Id, FirstName__c, Mother_of_Child__r.FirstName__c FROM Daughter__c WHERE Mother_of_Child__r.LastName__c LIKE 'C%'
- 從 parent object 為起點實作 outer JOIN:SELECT LastName__c, (SELECT LastName__c FROM Daughters__r) FROM Mother__c
[1] A Deeper Look at SOQL and Relationship Queries on Force.com
[2] Understanding Relationship Names
[3] Understanding Relationship Names, Custom Objects, and Custom Fields
沒有留言:
張貼留言