Welcome to the Cookbook

loading...

3.7.3.3 findBy

The original text for this section has changed since it was translated. Please help resolve this difference. You can:

More information about translations

findBy(string $value)

これらの魔法のような関数はあるフィールドを指定してテーブルを検索するためのショートカットとして使用することができます。関数名の最後にキャメルケース形式のフィールド名を追加し、第1引数としてそのフィールド用の判定基準を与えてください。

PHP5 findAllBy の例 対応する SQL
$this->Product->findAllByOrderStatus(‘3’); Product.order_status = 3
$this->Recipe->findAllByType(‘Cookie’); Recipe.type = ‘Cookie’
$this->User->findAllByLastName(‘Anderson’); User.last_name = ‘Anderson’
$this->Cake->findById(7); Cake.id = 7
$this->User->findByUserName(‘psychic’); User.user_name = ‘psychic’

PHP4 は、大文字と小文字を区別しないため、この関数の使用方法は少々異なります:

PHP4 findAllBy の例 対応する SQL
$this->Product->findAllByOrder_status(‘3’); Product.order_status = 3
$this->Recipe->findAllByType(‘Cookie’); Recipe.type = ‘Cookie’
$this->User->findAllByLast_name(‘Anderson’); User.last_name = ‘Anderson’
$this->Cake->findById(7); Cake.id = 7
$this->User->findByUser_name(‘psychic’); User.user_name = ‘psychic’

findBy() 関数は find('first',...) と、 findAllBy() 関数は find('all',...) と似ています。

いずれの場合でも、返り値は配列となり、 find() または findAll() を実行したときと同じ形式になります。