ここは旧式の4DドキュメントWebサイトです。最新のアップデートされたドキュメントを読むには新サイトをご利用下さい→ developer.4d.com

ホーム

 
4D v19.8
entitySelection.toCollection( )

entitySelection.toCollection( ) 


 

entitySelection.toCollection ( {filter ;}{ options {; begin {; howMany}}} ) -> 戻り値 
引数   説明
filter  文字, コレクション in どのエンティティのプロパティを取得するかを指定
options  倍長整数 in dk with primary key: プライマリーキーを追加
dk with stamp: スタンプを追加
begin  倍長整数 in 開始インデックスを指定
howMany  倍長整数 in 取得するエンティティ数
戻り値  コレクション in エンティティコレクションの属性と値を格納したオブジェクトのコレクション

説明   

entitySelection.toCollection( ) メソッドはエンティティコレクションの対応する属性名と値を、プロパティと値のセットとして格納しているオブジェクトを各要素として格納しているコレクションを作成し、返します。

filter 引数が省略されるか、空の文字列が渡されるか、"*" が渡された場合、全ての属性が取得されます。"リレートエンティティ" 型の"kind" プロパティの属性は単純な形式で取得されます: __KEY プロパティ(プライマリーキー)を持ったオブジェクトです。"リレートエンティティズ" 型の"kind" プロパティの属性は取得されません。

filter 引数には、取得したいエンティティの属性を渡すことができます。2つのシンタックスを使用できます:

  • プロパティパスの文字列をカンマで区切ったもの:"propertyPath1, propertyPath2, ..."
  • 文字列のコレクション: ["propertyPath1","propertyPath2",...]

filter 引数が、リレートエンティティ型の属性として指定されていた場合:

  • propertyPath = "relatedEntity" -> 単純な形式で取得
  • propertyPath = "relatedEntity.*" -> 全てのプロパティを取得
  • propertyPath = "relatedEntity.propertyName1, relatedEntity.propertyName2, ..." -> 指定されたプロパティのみ取得

filter 引数が、リレートエンティティズ型の属性として指定されていた場合:

  • propertyPath = "relatedEntities.*" -> 全てのプロパティを取得
  • propertyPath = "relatedEntities.propertyName1, relatedEntities.propertyName2, ..." -> 指定されたプロパティのみ取得

options 引数には、dk with primary key あるいは dk with stamp セレクターを渡すことで、エンティティのプライマリーキー、あるいはスタンプ(あるいはその両方)を、取得したオブジェクトに追加するかどうかを指定することができます。

begin 引数を使用すると取得するエンティティの開始インデックスを指定することができます。0 からエンティティセレクションの数-1の間の値を渡すことができます。

howMany 引数を使用すると、begin 引数で指定した位置から数えていくつのエンティティを取得するかということを指定することができます。ドロップされたエンティティは返されませんが、howMany 引数での数には考慮されます。例えば、howMany= 3 でドロップされたエンティティが1つある場合、2つのエンティティが取得されます。

howMany 引数がエンティティセレクションのlength を超える場合、メソッドは(length - begin) のオブジェクトを返します。

以下のいずれかの場合には空のコレクションが返されます:

  • エンティティセレクションが空である
  • begin 引数がエンティティセレクションのlength を超えている

以降のこのページでの例題では、全て以下のストラクチャーが使用されます:

filter あるいは options 引数を使用しない例:

 C_COLLECTION($employeesCollection)
 C_OBJECT($employees)
 
 $employeesCollection:=New collection
 $employees:=ds.Employee.all()
 $employeesCollection:=$employees.toCollection()

以下のものが返されます:

[
    {
        "ID": 416,
        "firstName": "Gregg",
        "lastName": "Wahl",
        "salary": 79100,
        "birthDate": "1963-02-01T00:00:00.000Z",
        "woman": false,
        "managerID": 412,
        "employerID": 20,
        "photo": "[object Picture]",
        "extra": null,
        "employer": {
            "__KEY": 20
        },
        "manager": {
            "__KEY": 412
        }
    },
    {
        "ID": 417,
        "firstName": "Irma",
        "lastName": "Durham",
        "salary": 47000,
        "birthDate": "1992-06-16T00:00:00.000Z",
        "woman": true,
        "managerID": 412,
        "employerID": 20,
        "photo": "[object Picture]",
        "extra": null,
        "employer": {
            "__KEY": 20
        },
        "manager": {
            "__KEY": 412
        }
    }
]

オプションを使用した場合の例:

 $employeesCollection:=New collection
 $employees:=ds.Employee.all()
 $employeesCollection:=$employees.toCollection("";dk with primary key+dk with stamp)

以下のものが返されます:

[
    {
        "__KEY": 416,
        "__STAMP": 1,
        "ID": 416,
        "firstName": "Gregg",
        "lastName": "Wahl",
        "salary": 79100,
        "birthDate": "1963-02-01T00:00:00.000Z",
        "woman": false,
        "managerID": 412,
        "employerID": 20,
        "photo": "[object Picture]",
        "extra": null,
        "employer": {
            "__KEY": 20
        },
        "manager": {
            "__KEY": 412
        }
    },
    {
        "__KEY": 417,
        "__STAMP": 1,
        "ID": 417,
        "firstName": "Irma",
        "lastName": "Durham",
        "salary": 47000,
        "birthDate": "1992-06-16T00:00:00.000Z",
        "woman": true,
        "managerID": 412,
        "employerID": 20,
        "photo": "[object Picture]",
        "extra": null,
        "employer": {
            "__KEY": 20
        },
        "manager": {
            "__KEY": 412
        }
    }]

filter を使用した場合の例 :

 $employeesCollection:=New collection
 $filter:=New collection
 $filter.push("firstName")
 $filter.push("lastName")
 
 $employees:=ds.Employee.all()
 $employeesCollection:=$employees.toCollection($filter;0;0;2)

以下のものが返されます:

[
    {
        "firstName": "Gregg",
        "lastName": "Wahl"
    },
    {
        "firstName": "Irma",
        "lastName": "Durham"
    }
]

リレートエンティティ型で単純な形式を使用した場合の例:

 $employeesCollection:=New collection
 $employeesCollection:=$employees.toCollection("firstName,lastName,employer")

以下のものが返されます:

[
    {
        "firstName": "Gregg",
        "lastName": "Wahl",
        "employer": {
            "__KEY": 20
        }
    },
    {
        "firstName": "Irma",
        "lastName": "Durham",
        "employer": {
            "__KEY": 20
        }
    },
    {
        "firstName": "Lorena",
        "lastName": "Boothe",
        "employer": {
            "__KEY": 20
        }
    }
   ]

コレクションのfilter 引数を使用した例:

 $employeesCollection:=New collection
 $coll:=New collection("firstName";"lastName")
 $employeesCollection:=$employees.toCollection($coll)

以下のものが返されます:

[
    {
        "firstName": "Joanna",
        "lastName": "Cabrera"
    },
    {
        "firstName": "Alexandra",
        "lastName": "Coleman"
    }
]

リレートエンティティ型の全てのプロパティを取得したい場合の例:

 $employeesCollection:=New collection
 $coll:=New collection
 $coll.push("firstName")
 $coll.push("lastName")
 $coll.push("employer.*")
 $employeesCollection:=$employees.toCollection($coll)

以下のものが返されます:

[
    {
        "firstName": "Gregg",
        "lastName": "Wahl",
        "employer": {
            "ID": 20,
            "name": "India Astral Secretary",
            "creationDate": "1984-08-25T00:00:00.000Z",
            "revenues": 12000000,
            "extra": null
        }
    },
    {
        "firstName": "Irma",
        "lastName": "Durham",
        "employer": {
            "ID": 20,
            "name": "India Astral Secretary",
            "creationDate": "1984-08-25T00:00:00.000Z",
            "revenues": 12000000,
            "extra": null
        }
    },
    {
        "firstName": "Lorena",
        "lastName": "Boothe",
        "employer": {
            "ID": 20,
            "name": "India Astral Secretary",
            "creationDate": "1984-08-25T00:00:00.000Z",
            "revenues": 12000000,
            "extra": null
        }
    }
  ]

リレートエンティティの一部のプロパティを取得したい場合の例:

 $employeesCollection:=New collection
 $employeesCollection:=$employees.toCollection("firstName, lastName, employer.name")

以下のものが返されます:

[
    {
        "firstName": "Gregg",
        "lastName": "Wahl",
        "employer": {
            "name": "India Astral Secretary"
        }
    },
    {
        "firstName": "Irma",
        "lastName": "Durham",
        "employer": {
            "name": "India Astral Secretary"
        }
    },
    {
        "firstName": "Lorena",
        "lastName": "Boothe",
        "employer": {
            "name": "India Astral Secretary"
        }
    }]

リレートエンティティズ型の一部のプロパティを取得する場合の例:

 $employeesCollection:=New collection
 $employeesCollection:=$employees.toCollection("firstName, lastName, directReports.firstName")

以下のものが返されます:

[
    {
        "firstName": "Gregg",
        "lastName": "Wahl",
        "directReports": []
    },
    {
        "firstName": "Mike",
        "lastName": "Phan",
        "directReports": [
            {
                "firstName": "Gary"
            },
            {
                "firstName": "Sadie"
            },
            {
                "firstName": "Christie"
            }
        ]
    },
    {
        "firstName": "Gary",
        "lastName": "Reichert",
        "directReports": [
            {
                "firstName": "Rex"
            },
            {
                "firstName": "Jenny"
            },
            {
                "firstName": "Lowell"
            }
        ]
    }]

リレートエンティティズ型の全てのプロパティを取得する場合の例:

 $employeesCollection:=New collection
 $employeesCollection:=$employees.toCollection("firstName, lastName, directReports.*")

以下のものが返されます:

[
    {
        "firstName": "Gregg",
        "lastName": "Wahl",
        "directReports": []
    },    
    {
        "firstName": "Mike",
        "lastName": "Phan",
        "directReports": [
            {
                "ID": 425,
                "firstName": "Gary",
                "lastName": "Reichert",
                "salary": 65800,
                "birthDate": "1957-12-23T00:00:00.000Z",
                "woman": false,
                "managerID": 424,
                "employerID": 21,
                "photo": "[object Picture]",
                "extra": null,
                "employer": {
                    "__KEY": 21
                },
                "manager": {
                    "__KEY": 424
                }
            },
            {
                "ID": 426,
                "firstName": "Sadie",
                "lastName": "Gallant",
                "salary": 35200,
                "birthDate": "2022-01-03T00:00:00.000Z",
                "woman": true,
                "managerID": 424,
                "employerID": 21,
                "photo": "[object Picture]",
                "extra": null,
                "employer": {
                    "__KEY": 21
                },
                "manager": {
                    "__KEY": 424
                }
            }
                   ]
    },
    {
        "firstName": "Gary",
        "lastName": "Reichert",
        "directReports": [
            {
                "ID": 428,
                "firstName": "Rex",
                "lastName": "Chance",
                "salary": 71600,
                "birthDate": "1968-08-09T00:00:00.000Z",
                "woman": false,
                "managerID": 425,
                "employerID": 21,
                "photo": "[object Picture]",
                "extra": null,
                "employer": {
                    "__KEY": 21
                },
                "manager": {
                    "__KEY": 425
                }
            },
            {
                "ID": 429,
                "firstName": "Jenny",
                "lastName": "Parks",
                "salary": 51300,
                "birthDate": "1984-05-25T00:00:00.000Z",
                "woman": true,
                "managerID": 425,
                "employerID": 21,
                "photo": "[object Picture]",
                "extra": null,
                "employer": {
                    "__KEY": 21
                },
                "manager": {
                    "__KEY": 425
                }
            }
           ]
 }
]



参照 

dataClass.fromCollection( )

 
プロパティ 

プロダクト: 4D
テーマ: ORDA - エンティティセレクション

 
ページの目次 
 
履歴 

初出: 4D v17

 
ARTICLE USAGE

ランゲージリファレンス ( 4D v19)
ランゲージリファレンス ( 4D v19.1)
ランゲージリファレンス ( 4D v19.4)
ランゲージリファレンス ( 4D v19.5)
ランゲージリファレンス ( 4D v19.6)
ランゲージリファレンス ( 4D v19.7)
ランゲージリファレンス ( 4D v19.8)