Cakephp Classroom image

osdyui
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18|Last
Lessons:-Models

)
)
);
$options[’conditions’] = array(
’Channel.private’ => 1
);
$privateItems = $Item->find(’all’, $options);
You could perform several joins as needed in hasAndBelongsToMany:
Suppose a Book hasAndBelongsToMany Tag association. This relation uses a books_tags table as join table,
so you need to join the books table to the books_tags table, and this with the tags table:
$options[’joins’] = array(
array(’table’ => ’books_tags’,
’alias’ => ’BooksTag’,
’type’ => ’inner’,
’conditions’ => array(
’Books.id = BooksTag.books_id’
)
),
array(’table’ => ’tags’,
’alias’ => ’Tag’,
’type’ => ’inner’,
’conditions’ => array(
’BooksTag.tag_id = Tag.id’
)
)
);
$options[’conditions’] = array(
’Tag.tag’ => ’Novel’
);
$books = $Book->find(’all’, $options);
Using joins allows you to have a maximum flexibility in how CakePHP handles associations and fetch the
data, however in most cases you can use other tools to achieve the same results such as correctly defining
associations, binding models on the fly and using the Containable behavior. This feature should be used
with care because it could lead, in a few cases, into bad formed SQL queries if combined with any of the
former techniques described for associating models.
Retrieving Your Data
As stated before, one of the roles of the Model layer is to get data from multiple types of storage. The
CakePHP Model class comes with some functions that will help you search for this data, sort it, paginate it,
and filter it. The most common function you will use in models is Model::find()find
find(string $type = ’first’, array $params = array())
Find is the multifunctional workhorse of all model data-retrieval functions. $type can be either ’all’,
’first’, ’count’, ’list’, ’neighbors’ or ’threaded’ or any custom finder you can define.
Keep in mind that $type is case sensitive. Using an 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’
)
It’s also possible to add and use other parameters, as is made use of by some find types, behaviors and of
course possibly with your own model methods.
find(‘first’)
find(’first’, $params) will return one result, you’d use this for any case where you expect only
one result. Below are a couple of simple (controller code) examples:
public function some_function() {
// ...
$semiRandomArticle = $this->Article->find(’first’);
$lastCreated = $this->Article->find(’first’, array(
’order’ => array(’Article.created’ => ’desc’)
));
$specificallyThisOne = $this->Article->find(’first’, array(
’conditions’ => array(’Article.id’ => 1)
));
// ...
}
In the first example, no parameters at all are passed to find - therefore no conditions or sort order will be
used. The format returned from find(’first’) call is of the form:
Array
(
[ModelName] => Array
(
[id] => 83
[field1] => value1

 
 
 

osdyui

Skills    Cakephp

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

  Students (0)