You can retrieve data from database by following
built-in functions :- 1- find()
2- findAllBy()
3- findBy()
1-
find($type, $params)->
Find is the multifunctional workhorse of all model data-retrieval functions.
$type
can be either 'all'
, 'first'
, 'count'
, 'list'
, 'neighbors'
or 'threaded'
. The default find type is 'first'
. Keep in mind that $type
is case sensitive. Using a upper case character (for example 'All'
) will not produce the expected results.$params
is used to pass all parameters to the various finds, and has the following possible keys by default - all of which are optional:
array(
'conditions' => array('Model.field' => $thisValue), //array of conditions
'recursive' => 1, //int
'fields' => array('Model.field1', 'DISTINCT Model.field2'), //array of field names
'order' => array('Model.created', 'Model.field3 DESC'), //string or array defining order
'group' => array('Model.field'), //fields to GROUP BY
'limit' => n, //int
'page' => n, //int
'offset'=>n, //int
'callbacks' => true //other possible values are false, 'before', 'after'
)
Types :-
(i)
find('first') - 'first' is the default find type, and will return one result, it will return only one row of the table. Example- Article
is any table in your databasefunction any_function() {
...
$this->Article->find('first', array('order' => array('Article.created DESC')));
}
this query return the result in the following manner--
Array ( [ModelName] => Array ( [id] => 83 [field1] => value1 [field2] => value2 [field3] => value3 )
}
(ii) find('count')-> it returns an integer value.
$stat= $this->Article->find('count', array('conditions' =>
array('Article.status' => 'pending')));
here $stat variable contain a integer value i.e. equal to 0 or greater
then 0.
(iii) find('all')-> it
returns an array of (potentially multiple) results
and it also show the value from different table that are related
to main table's primary key.
$allArticles = $this->Article->find('all');
$pending = $this->Article->find('all', array('conditions' =>
array('Article.status' => 'pending')));
(iv) find('list')-> it returns an indexed array, useful for any use where
you would want a list . By default it return 'id' and first 'column' of
table.
$allArticles = $this->Article->find('list');
$pending = $this->Article->find('list', array(
'conditions' => array('Article.status' => 'pending')
;
The results of a call to
find('list')
will be in the following form:Array ( //[id] => 'displayValue', [1] => 'displayValue1', [2] => 'displayValue2', [4] => 'displayValue4', [5] => 'displayValue5', [6] => 'displayValue6', [3] => 'displayValue3', )You ca change the list's field according to you as by giving fields name :
$usernameMap = $this->Article->User->find('list',
array('fields' => array('User.username', 'User.first_name')));
2- findAllBy()->
findAllBy<fieldName>(string $value,array $fields,array $order,int $limit,
int $page, int $recursive)
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.
PHP findALLBy SQL Fragment
$this->Product->findAllByOrderStatus(‘3’); Product.order_status = 3 $this->User->findAllByLastName(‘Anderson’); User.last_name =‘Anderson’
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.
PHP findALLBy SQL Fragment
$this->Product->findByOrderStatus(‘3’); Product.order_status = 3
$this->User->findByLastName(‘Anderson’); User.last_name =‘Anderson’