3.7.3.3 findBy
findBy<fieldName>(string $value)
These magic functions can be used as a shortcut to search your tables by a certain field. Just add the name of the field (in CamelCase format) to the end of these functions, and supply the criteria for that field as the first parameter.
| PHP5 findBy<x> Example | Corresponding SQL Fragment |
|---|---|
| $this->Product->findByOrderStatus(‘3’); | Product.order_status = 3 |
| $this->Recipe->findByType(‘Cookie’); | Recipe.type = ‘Cookie’ |
| $this->User->findByLastName(‘Anderson’); | User.last_name = ‘Anderson’ |
| $this->Cake->findById(7); | Cake.id = 7 |
| $this->User->findByUserName(‘psychic’); | User.user_name = ‘psychic’ |
PHP4 users have to use this function a little differently due to some case-insensitivity in PHP4:
| PHP4 findBy<x> Example | Corresponding SQL Fragment |
|---|---|
| $this->Product->findByOrder_status(‘3’); | Product.order_status = 3 |
| $this->Recipe->findByType(‘Cookie’); | Recipe.type = ‘Cookie’ |
| $this->User->findByLast_name(‘Anderson’); | User.last_name = ‘Anderson’ |
| $this->Cake->findById(7); | Cake.id = 7 |
| $this->User->findByUser_name(‘psychic’); | User.user_name = ‘psychic’ |
findBy() functions like find('first',...), while findAllBy() functions like find('all',...).
In either case, the returned result is an array formatted just as it would be from find() or findAll(), respectively.
