The find() method (public function find($key=0,$options=NULL)) provides three ways of searching:
$object->find($id) will return a single associative array of the record matching the given ID.
Example: $message=$guestbook->find($_GET['id']);
$object->find(array("field"=>"some_field", "value"=>"some value")); will return single associative array based on field/value pair that you choose.
Example: $user=$user_object->find(array("field"=>"email", "value"=>"john@yahoo.com"));
There are ways to retrieve multiple records. Calling find() without any parameters returns all the results in the table paginated with the total count.
Example: list($users,$count_users)=$user_object->find();
Thus in $users you will have all the records and you can use foreach, for or other operator to iterate through them. In $count_users you will have the total count that might be needed for pagination function or anything else.
If you don't want paginated content or want to add conditions, you can pass parameters to the find() method. To receive such multiple values you need to pass 0 to the first parameter and then array of conditions.
Examples: a) list($users, $count_users) = $object->find(0, array("page_limit"=>20));
will return all users but paginated by 20 instead of the default PAGE_LIMIT setting.
b) list($users, $count_users) = $object->find(0, array("conditions"=>" gender='male' AND age>18 "));
will return males above 18 years.
The $options array allows passing also:
- "offset" - the offset for pagination, if such is not provided the system will try to find $_GET['offset']
- "orderby" - defaults to "id"
- "orderdir" - defaults to "ASC"
Finally, if you don't want paginated results and total count, you can pass -1 as "page_limit" to get only an array:
$rows=$object->find(0,array("page_limit"=>-1));
Example: $users=$user_object->find(0,array("page_limit"=>-1)); |