PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ja/core-libraries/collections.rst

https://gitlab.com/albertkeba/docs
ReStructuredText | 260 lines | 188 code | 72 blank | 0 comment | 0 complexity | aae644d4e4f3c1682ec1e75154ade232 MD5 | raw file
  1. .. Collections
  2. コレクション
  3. ############
  4. ..
  5. Components, Helpers, Behaviors and Tasks all share a similar structure and set
  6. of behaviors. For 2.0, they were given a unified API for interacting with
  7. collections of similar objects. The collection objects in CakePHP, give you
  8. a uniform way to interact with several different kinds of objects in your
  9. application.
  10. コンポーネントヘルパービヘイビアそしてタスクは皆似たような構造と一連の振る舞いを共有しています
  11. 2.0では同種のオブジェクトのコレクションとやり取りできる統一APIがそれらに与えられました
  12. CakePHPのこのコレクションオブジェクトはアプリケーション内の様々な異なる種類のオブジェクトとのやり取りにおいて統一された方法を提供します
  13. ..
  14. While the examples below, will use Components, the same behavior can be expected
  15. for Helpers, Behaviors, and Tasks in addition to Components.
  16. 以下においてはコンポーネントを例に用いることになりますが同じ振舞いはコンポーネントの他にもヘルパービヘイビアそしてタスクに期待出来ます
  17. .. Loading and unloading objects
  18. オブジェクトのロードとアンロード
  19. ================================
  20. ..
  21. Loading objects on every kind of collection can be done using the ``load()``
  22. method::
  23. すべての種類のコレクションにおいてオブジェクトのロードは ``load()`` メソッドを使えば可能になります::
  24. $this->Prg = $this->Components->load('Prg');
  25. $this->Prg->process();
  26. ..
  27. When loading a component, if the component is not currently loaded into the
  28. collection, a new instance will be created. If the component is already loaded,
  29. another instance will not be created. When loading components, you can also
  30. provide additional configuration for them::
  31. あるコンポーネントをロードする時点においてそのコンポーネントがコレクションにロードされていないなら新しいインスタンスが生成されます
  32. もし既にそのコンポーネントがロードされているなら別のインスタンスは新たに生成されません
  33. ロードする際にコンポーネントに設定を追加することもできます::
  34. $this->Cookie = $this->Components->load('Cookie', array('name' => 'sweet'));
  35. ..
  36. Any keys & values provided will be passed to the Component's constructor. The
  37. one exception to this rule is ``className``. ClassName is a special key that is
  38. used to alias objects in a collection. This allows you to have component names
  39. that do not reflect the classnames, which can be helpful when extending core
  40. components::
  41. 設定を追加したどんなキーも値もコンポーネントのコンストラクタに渡されることになるでしょう
  42. このルールの一つの例外は ``className`` です ``className`` コレクションにおけるオブジェクトのエイリアスとして用いられる特別なキーです
  43. これによりクラス名を反映させないコンポーネント名コアのコンポーネントを拡張する際に便利ですを使えるようになります::
  44. $this->Auth = $this->Components->load('Auth', array('className' => 'MyCustomAuth'));
  45. $this->Auth->user(); // 実際には MyCustomAuth::user() が使用されます
  46. ..
  47. The inverse of loading an object, is unloading it. Unloaded objects are removed
  48. from memory, and will not have additional callbacks triggered on them::
  49. オブジェクトのロードの逆を行うことそれがアンロードするということです
  50. アンロードされたオブジェクトはメモリから消去されそれによりトリガーされるコールバックを追加されることもありません::
  51. $this->Components->unload('Cookie');
  52. $this->Cookie->read(); // Fatal error.
  53. .. Triggering callbacks
  54. コールバックのトリガー
  55. ======================
  56. ..
  57. Callbacks are supported by collection objects. When a collection has a callback
  58. triggered, that method will be called on all enabled objects in the collection.
  59. You can pass parameters to the callback loop as well::
  60. コレクションオブジェクトはコールバックをサポートします
  61. トリガーされたコールバックをコレクションが持っているとそのメソッドはコレクション内の実行可能enabledなすべてのオブジェクトに対してコールされます
  62. さらにパラメータをコールバックのループに渡すことも出来ます::
  63. $this->Behaviors->trigger('afterFind', array($this, $results, $primary));
  64. ..
  65. In the above ``$this`` would be passed as the first argument to every
  66. behavior's afterFind method. There are several options that can be used to
  67. control how callbacks are fired:
  68. 上の例において ``$this`` は全てのビヘイビアの afterFind メソッドに第一引数として渡されます
  69. コールバックの起動の仕方を制御できる様々なオプションがあります:
  70. ..
  71. - ``breakOn`` Set to the value or values you want the callback propagation to stop on.
  72. Can either be a scalar value, or an array of values to break on. Defaults to ``false``.
  73. - ``breakOn`` コールバックの伝播を停止させるための値一つでも複数でも好きなだけをセットします
  74. 停止条件はスカラー値でも配列でも構いませんデフォルトは ``false``
  75. ..
  76. - ``break`` Set to true to enabled breaking. When a trigger is broken, the last returned value
  77. will be returned. If used in combination with ``collectReturn`` the collected results will be returned.
  78. Defaults to ``false``.
  79. - ``break`` で停止できるかどうかを設定しますトリガーが破棄されたときは最後の戻り値が戻されます
  80. ``collectReturn`` と組み合わせて用いられたときは複数の結果がまとめて返されます
  81. デフォルトは ``false``
  82. ..
  83. - ``collectReturn`` Set to true to collect the return of each object into an array.
  84. This array of return values will be returned from the trigger() call. Defaults to ``false``.
  85. - ``collectReturn`` 各々のオブジェクトの戻り値を配列に格納するために true をセットします
  86. この配列は trigger() メソッドの呼び出しから返されますデフォルトは ``false``
  87. ..
  88. - ``triggerDisabled`` Will trigger the callback on all objects in the collection even the non-enabled
  89. objects. Defaults to false.
  90. - ``triggerDisabled`` コレクション内の無効化したオブジェクトに対してさえも全てコールバックをトリガーします
  91. デフォルトは ``false``
  92. ..
  93. - ``modParams`` Allows each object the callback gets called on to modify the parameters to the next object.
  94. Setting modParams to an integer value will allow you to modify the parameter with that index.
  95. Any non-null value will modify the parameter index indicated.
  96. Defaults to false.
  97. - ``modParams`` コールバックに渡された各オブジェクトに次のオブジェクトへ渡されるパラメータに変更を加えることを許可します
  98. modParams を整数値に設定している場合それをインデックスとしたパラメータに対する変更を許可します
  99. 非NULLの値なら何であれそのインデクスが指し示すパラメータの変更を許可します
  100. デフォルトは ``false``
  101. .. Canceling a callback loop
  102. コールバックループのキャンセル
  103. ------------------------------
  104. ..
  105. Using the ``break`` and ``breakOn`` options you can cancel a callback loop
  106. midway similar to stopping event propagation in JavaScript::
  107. ``break`` ``breakOn`` オプションを用いることでコールバックループを JavaScript におけるイベント伝播を停止させるような感じで途中でキャンセルすることができます::
  108. $this->Behaviors->trigger(
  109. 'beforeFind',
  110. array($this, $query),
  111. array('break' => true, 'breakOn' => false)
  112. );
  113. ..
  114. In the above example, if any behavior returns ``false`` from its beforeFind
  115. method, no further callbacks will be called. In addition, the return of
  116. ``trigger()`` will be false.
  117. 上の例では全てのビヘイビアが beforeFind メソッドから ``false`` を返すならそれ以上コールバックが呼ばれることはありません
  118. ちなみに ``trigger()`` の戻り値は false になるはずです
  119. .. Enabling and disabling objects
  120. オブジェクトの有効化と無効化
  121. ============================
  122. ..
  123. Once an object is loaded into a collection you may need to disable it.
  124. Disabling an object in a collection prevents future callbacks from being fired
  125. on that object unless the ``triggerDisabled`` option is used::
  126. 一度コレクションにロードしたオブジェクトを無効化したくなることがあるかもしれません
  127. コレクション内のオブジェクトを無効化することでそれ以降 ``triggerDisabled`` オプションを用いない場合のコールバックの発火を防ぎます::
  128. // HtmlHelper を無効化
  129. $this->Helpers->disable('Html');
  130. // その後、そのヘルパーを再有効化
  131. $this->Helpers->enable('Html');
  132. ..
  133. Disabled objects can still have their normal methods and properties used. The
  134. primary difference between an enabled and disabled object is with regards to
  135. callbacks. You can interrogate a collection about the enabled objects, or check
  136. if a specific object is still enabled using ``enabled()``::
  137. 無効化されたオブジェクトはまだそれらの通常のメソッドとプロパティを持っています
  138. 有効状態と無効状態の根本的な違いはコールバックに関してです
  139. ``enabled()`` メソッドを用いることで有効オブジェクトに何があるのかを問い合わせたり特定のオブジェクトがまだ有効であるのかをチェックすることが出来ます::
  140. // あるヘルパーが有効かどうかをチェックします
  141. $this->Helpers->enabled('Html');
  142. // $enabled はこの時点で有効なヘルパーの配列です
  143. $enabled = $this->Helpers->enabled();
  144. .. Object callback priorities
  145. オブジェクトのコールバックのプロパティ
  146. ======================================
  147. ..
  148. You can prioritize the triggering object callbacks similar to event callbacks.
  149. The handling of priority values and order of triggering is the same as
  150. explained :ref:`here <event-priorities>`.
  151. Here's how you can specify priority at declaration time::
  152. イベントコールバックに対して行うのと同じようにオブジェクトコールバックをトリガーする順番を指定することが出来ます
  153. プライオリティ値とトリガーの順番との取り扱いは :ref:`ここ <event-priorities>` の説明と同じです
  154. 宣言時にプライオリティを設定できる方法は次に示します::
  155. class SomeController {
  156. public $components = array(
  157. 'Foo', //Foo はデフォルトのプライオリティ 10 を持ちます
  158. 'Bar' => array('priority' => 9) //Bar のコールバックは Foo's の前にトリガーされます
  159. );
  160. public $helpers = array(
  161. 'Cache' => array('priority' => 12), //Cache のコールバックは最後に
  162. //トリガーされるでしょう
  163. 'Asset',
  164. 'Utility' //Utility は Asset と同じプライオリティ 10 を持ち、そのコールバックは
  165. //Asset のものよりあとにトリガーされます
  166. );
  167. }
  168. class Post {
  169. public $actsAs = array(
  170. 'DoFirst' => array('priority' => 1),
  171. 'Media'
  172. );
  173. }
  174. ..
  175. When dynamically loading objects to a collection you can specify the priority like this::
  176. コレクションへ動的にオブジェクトをロードした場合こんな感じで優先順位を指定出来ます::
  177. $this->MyComponent = $this->Components->load('MyComponent', array('priority' => 9));
  178. ..
  179. You can also change priorities at run time using the ``ObjectCollection::setPriority()`` function::
  180. ``ObjectCollection::setPriority()`` メソッドを用いることで
  181. 実行途中に優先順位を変更することも出来ます::
  182. //オブジェクトがひとつの場合
  183. $this->Components->setPriority('Foo', 2);
  184. //オブジェクトが複数の場合
  185. $this->Behaviors->setPriority(array('Object1' => 8, 'Object2' => 9));
  186. .. meta::
  187. :title lang=ja: コレクション
  188. :keywords lang=ja: array name,loading components,several different kinds,unified api,loading objects,component names,special key,core components,callbacks,prg,callback,alias,fatal error,collections,memory,priority,priorities