Cakephp Classroom image

pankaj
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21|Last
Lessons:-Models

Warning: If you specify fields, you need to always include the parent_id (or its current alias):
public function some_function() {
$categories = $this->Category->find(’threaded’, array(
’fields’ => array(’id’, ’name’, ’parent_id’)
));
}
Otherwise the returned array will not be of the expected nested structure from above.
find(‘neighbors’)
find(’neighbors’, $params) 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:
public function some_function() {
$neighbors = $this->Article->find(’neighbors’, array(’field’ => ’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: Note how the result always contains only two root elements: prev and next. This function does not
honor a model’s default recursive var. The recursive setting must be passed in the parameters on each call.
Creating custom find types
The find method is flexible enough to accept your custom finders, this is done by declaring your own types
in a model variable and by implementing a special function in your model class.
A Model Find Type is a shortcut to find options. For example, the following two finds are equivalent
$this->User->find(’first’);
$this->User->find(’all’, array(’limit’ => 1));
The following are core find types:
• first
• all
• count
• list
• threaded
• neighbors
But what about other types? Let’s say you want a finder for all published articles in your database. The first
change you need to do is add your type to the Model::$findMethods variable in the model
class Article extends AppModel {
public $findMethods = array(’available’ => true);
}
Basically this is just telling CakePHP to accept the value available as the first argument of the find
function. Next step is to implement the function _findAvailable. This is done by convention, if you
wanted to implement a finder called myFancySearch then the method to implement would be named
_findMyFancySearch.
class Article extends AppModel {
public $findMethods = array(’available’ => true);
protected function _findAvailable($state, $query, $results = array()) {

 
 
 

pankaj

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)