3.7.3.1 find
The original text for this section has changed since it was translated. Please help resolve this difference. You can:
find($type, $params)
findはモデルのデータを検索するための多くの機能を持った働き者です。$type は 'all'、'first'、'count'、'list'、'neighbors' または 'threaded' のいずれかです。デフォルトは 'first' です。$typeは大文字と小文字を区別することを覚えておいてください。大文字を使うと(例:'All')、期待する結果を得られないことがあります。
$params はオプションの配列です。キーとして次のものが有効です。:
array(
'conditions' => array('Model.field' => $thisValue), //条件の配列
'recursive' => 1, //int
'fields' => array('Model.field1', 'DISTINCT Model.field2'), //フィールド名の配列
'order' => array('Model.created', 'Model.field3 DESC'), //文字列か配列でのorder定義
'group' => array('Model.field'), //GROUP BYするためのフィールド
'limit' => n, //int
'page' => n, //int
'offset' => n, //int
'callbacks' => true //false, 'before', 'after'が指定できます。
) array('conditions' => array('Model.field' => $thisValue), //条件の配列'recursive' => 1, //int'fields' => array('Model.field1', 'DISTINCT Model.field2'), //フィールド名の配列'order' => array('Model.created', 'Model.field3 DESC'), //文字列か配列でのorder定義'group' => array('Model.field'), //GROUP BYするためのフィールド'limit' => n, //int'page' => n, //int'offset' => n, //int'callbacks' => true //false, 'before', 'after'が指定できます。)
また、いくつかのタイプのfind、ビヘイビアで使われる他のパラメータも指定することができます。もちろん独自のモデルメソッドにおいても可能です。
有効なモデルのコールバックに関する詳細は、こちら を参照してください。
3.7.3.1.1 find('first')
find('first', $params)
'first' は find のデフォルトの型で、1件の結果を返します。1件だけ結果が欲しい時は、これを使ってください。コントローラ中で使う場合の簡単な例を、いくつか次に示します。
function some_function() {
...
$this->Article->order = null; // これがセットされているとリセットされます
$semiRandomArticle = $this->Article->find();
$this->Article->order = 'Article.created DESC'; // モデルがデフォルトの並び順を持つようにシミュレートします
$lastCreated = $this->Article->find();
$alsoLastCreated = $this->Article->find('first', array('order' => array('Article.created DESC')));
$specificallyThisOne = $this->Article->find('first', array('conditions' => array('Article.id' => 1)));
...
}
function some_function() {...$this->Article->order = null; // これがセットされているとリセットされます$semiRandomArticle = $this->Article->find();$this->Article->order = 'Article.created DESC'; // モデルがデフォルトの並び順を持つようにシミュレートします$lastCreated = $this->Article->find();$alsoLastCreated = $this->Article->find('first', array('order' => array('Article.created DESC')));$specificallyThisOne = $this->Article->find('first', array('conditions' => array('Article.id' => 1)));...}
最初の例では、find にパラメータが一切渡されていません。したがって、検索条件と並び順が使用されません。find('first') が返す値は、次のような形式になります。
Array
(
[ModelName] => Array
(
[id] => 83
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 1
[field1] => value1
[field2] => value2
[field3] => value3
)
)
find('first') において使用する追加のパラメータは、前述したもので全てです。
3.7.3.1.2 find('count')
find('count', $params)
find('count', $params) は整数型の値を返します。コントローラ中で使う場合の簡単な例を、いくつか次に示します。
function some_function() {
...
$total = $this->Article->find('count');
$pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));
$authors = $this->Article->User->find('count');
$publishedAuthors = $this->Article->find('count', array(
'fields' => 'COUNT(DISTINCT Article.user_id) as count',
'conditions' => array('Article.status !=' => 'pending')
));
...
}
function some_function() {...$total = $this->Article->find('count');$pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));$authors = $this->Article->User->find('count');$publishedAuthors = $this->Article->find('count', array('fields' => 'COUNT(DISTINCT Article.user_id) as count','conditions' => array('Article.status !=' => 'pending')));...}
find('count') では、 fields に配列を渡してはいけません。DISTINCT カウントを行うフィールドだけを指定するようにしてください。そうすることで、条件に従った結果が常に同じになります。
find('count') において使用する追加のパラメータは、前述したもので全てです。
3.7.3.1.3 find('all')
find('all', $params)
find('all')は(複数になりうる)結果の配列を返します。このメカニズムは実に全ての find() の別種類に使われ、またpaginateにも使われます。以下はいくつかの単純な(コントローラのコードの)サンプルです。
function some_function() {
...
$allArticles = $this->Article->find('all');
$pending = $this->Article->find('all', array('conditions' => array('Article.status' => 'pending')));
$allAuthors = $this->Article->User->find('all');
$allPublishedAuthors = $this->Article->User->find('all', array('conditions' => array('Article.status !=' => 'pending')));
...
}
function some_function() {...$allArticles = $this->Article->find('all');$pending = $this->Article->find('all', array('conditions' => array('Article.status' => 'pending')));$allAuthors = $this->Article->User->find('all');$allPublishedAuthors = $this->Article->User->find('all', array('conditions' => array('Article.status !=' => 'pending')));...}
上記のサンプルでは、 $allAuthors は users テーブルの全てのユーザに等しくなります。find に何も(訳注:パラメータが)与えられないと、find には条件が何も適用されません。
find('all')の呼び出しの結果は以下のようなフォーマットになることでしょう。
Array
(
[0] => Array
(
[ModelName] => Array
(
[id] => 83
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 1
[field1] => value1
[field2] => value2
[field3] => value3
)
)
)
find('all')のためだけに使うパラメータはありません。
3.7.3.1.4 find('list')
The original text for this section has changed since it was translated. Please help resolve this difference. You can:
find('list', $params)
find('list', $params)はセレクトボックスを生成するためのリストなどに便利な、インデックス化された配列を返します。以下はいくつかの単純な(コントローラのコードの)サンプルです。
function some_function() {
...
$allArticles = $this->Article->find('list');
$pending = $this->Article->find('list', array('conditions' => array('Article.status' => 'pending')));
$allAuthors = $this->Article->User->find('list');
$allPublishedAuthors = $this->Article->User->find('list', array('conditions' => array('Article.status !=' => 'pending')));
...
}
function some_function() {...$allArticles = $this->Article->find('list');$pending = $this->Article->find('list', array('conditions' => array('Article.status' => 'pending')));$allAuthors = $this->Article->User->find('list');$allPublishedAuthors = $this->Article->User->find('list', array('conditions' => array('Article.status !=' => 'pending')));...}
上記のサンプルでは、$allAuthors は users テーブルの全てのユーザに等しくなります。find に何も(訳注:パラメータが)与えられないと、find には条件が何も適用されません。
find('list')の呼び出しの結果は以下のようなフォーマットになることでしょう。
Array
(
//[id] => 'displayValue',
[1] => 'displayValue1',
[2] => 'displayValue2',
[4] => 'displayValue4',
[5] => 'displayValue5',
[6] => 'displayValue6',
[3] => 'displayValue3',
)
find('list')を呼び出すとき、与えられたfieldsは返り値の配列のキーと値、もしあれば結果をグルーピングするためのもの、を決定付けるものとして使用されます。デフォルトではモデルの主キーがキーとして、display field(モデルの属性displayFieldで設定できる)が値として扱われます。以下はより具体的ないくつかの例です。
function some_function() {
...
$justusernames = $this->Article->User->find('list', array('fields' => array('User.username')));
$usernameMap = $this->Article->User->find('list', array('fields' => array('User.username', 'User.first_name')));
$usernameGroups = $this->Article->User->find('list', array('fields' => array('User.username', 'User.first_name', 'User.group')));
...
}
function some_function() {...$justusernames = $this->Article->User->find('list', array('fields' => array('User.username')));$usernameMap = $this->Article->User->find('list', array('fields' => array('User.username', 'User.first_name')));$usernameGroups = $this->Article->User->find('list', array('fields' => array('User.username', 'User.first_name', 'User.group')));...}
上記のコードの例の結果、変数は以下のようになるでしょう。
$justusernames = Array
(
//[id] => 'username',
[213] => 'AD7six',
[25] => '_psychic_',
[1] => 'PHPNut',
[2] => 'gwoo',
[400] => 'jperras',
)
$usernameMap = Array
(
//[username] => 'firstname',
['AD7six'] => 'Andy',
['_psychic_'] => 'John',
['PHPNut'] => 'Larry',
['gwoo'] => 'Gwoo',
['jperras'] => 'Joël',
)
$usernameGroups = Array
(
['Uber'] => Array
(
['PHPNut'] => 'Larry',
['gwoo'] => 'Gwoo',
)
['Admin'] => Array
(
['_psychic_'] => 'John',
['AD7six'] => 'Andy',
['jperras'] => 'Joël',
)
)
3.7.3.1.5 find('threaded')
このセクションには保留されている変更があります. More information about translations
find('threaded', $params)
find('threaded', $params) returns a nested array, and is appropriate if you want to use the parent_id field of your model data to build nested results. Below are a couple of simple (controller code) examples:
function some_function() {
...
$allCategories = $this->Category->find('threaded');
$aCategory = $this->Category->find('first', array('conditions' => array('parent_id' => 42))); // not the root
$someCategories = $this->Category->find('threaded', array(
'conditions' => array(
'Article.lft >=' => $aCategory['Category']['lft'],
'Article.rght <=' => $aCategory['Category']['rght']
)
));
...
}
function some_function() {...$allCategories = $this->Category->find('threaded');$aCategory = $this->Category->find('first', array('conditions' => array('parent_id' => 42))); // not the root$someCategories = $this->Category->find('threaded', array('conditions' => array('Article.lft >=' => $aCategory['Category']['lft'],'Article.rght <=' => $aCategory['Category']['rght'])));...}
It is not necessary to use the Tree behavior to use this method - but all desired results must be possible to be found in a single query.
In the above code example, $allCategories will contain a nested array representing the whole category structure. The second example makes use of the data structure used by the Tree behavior the return a partial, nested, result for $aCategory and everything below it. The results of a call to find('threaded') will be of the following form:
Array
(
[0] => Array
(
[ModelName] => Array
(
[id] => 83
[parent_id] => null
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 1
[field1] => value1
[field2] => value2
[field3] => value3
)
[children] => Array
(
[0] => Array
(
[ModelName] => Array
(
[id] => 42
[parent_id] => 83
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 2
[field1] => value1
[field2] => value2
[field3] => value3
)
[children] => Array
(
)
)
...
)
)
)
The order results appear can be changed as it is influence by the order of processing. For example, if 'order' => 'name ASC' is passed in the params to find('threaded'), the results will appear in name order. Likewise any order can be used, there is no inbuilt requirement of this method for the top result to be returned first.
There are no additional parameters used by find('threaded').
3.7.3.1.6 find('neighbors')
このセクションには保留されている変更があります. More information about translations
find('neighbors', $params)
'neighbors' will perform a find similar to 'first', but will return the row before and after the one you request. Below is a simple (controller code) example:
function some_function() {
$neighbors = $this->Article->find('neighbors', array('fields' => 'id', 'value' => 3));
}
function some_function() {$neighbors = $this->Article->find('neighbors', array('fields' => 'id', 'value' => 3));}
You can see in this example the two required elements of the $params array: field and value. Other elements are still allowed as with any other find (Ex: If your model acts as containable, then you can specify 'contain' in $params). The format returned from a find('neighbors') call is in the form:
Array
(
[prev] => Array
(
[ModelName] => Array
(
[id] => 2
[field1] => value1
[field2] => value2
...
)
[AssociatedModelName] => Array
(
[id] => 151
[field1] => value1
[field2] => value2
...
)
)
[next] => Array
(
[ModelName] => Array
(
[id] => 4
[field1] => value1
[field2] => value2
...
)
[AssociatedModelName] => Array
(
[id] => 122
[field1] => value1
[field2] => value2
...
)
)
)
Note how the result always contains only two root elements: prev and next.

