Cakephp Classroom image

pankaj
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11|Last
Lessons:-Models

Once this association has been defined, find operations on the User model will also fetch related Comment
records if they exist:
//Sample results from a $this->User->find() call.
Array
(
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
[Comment] => Array
(
[0] => Array
(
[id] => 123
[user_id] => 121
[title] => On Gwoo the Kungwoo
[body] => The Kungwooness is not so Gwooish
[created] => 2006-05-01 10:31:01
)
[1] => Array
(
[id] => 124
[user_id] => 121
[title] => More on Gwoo
[body] => But what of the ‘ Nut?
[created] => 2006-05-01 10:41:01
)
)
)
One thing to remember is that you’ll need a complimentary Comment belongsTo User association in order
to get the data from both directions. What we’ve outlined in this section empowers you to get Comment data
from the User. Adding the Comment belongsTo User association in the Comment model empowers you to
get User data from the Comment model - completing the connection and allowing the flow of information
from either model’s perspective.
counterCache - Cache your count()
This function helps you cache the count of related data. Instead of counting the records manually via
find(’count’), the model itself tracks any addition/deleting towards the associated $hasMany model
and increases/decreases a dedicated integer field within the parent model table.
The name of the field consists of the singular model name followed by a underscore and the word “count”:
my_model_count
Let’s say you have a model called ImageComment and a model called Image, you would add a new
INT-field to the images table and name it image_comment_count.Here are some more examples:
Model Associated Model Example
User Image users.image_count
Image ImageComment images.image_comment_count
BlogEntry BlogEntryComment blog_entries.blog_entry_comment_count
Once you have added the counter field you are good to go. Activate counter-cache in your association by
adding a counterCache key and set the value to true:
class ImageComment extends AppModel {
public $belongsTo = array(
’Image’ => array(
’counterCache’ => true,
)
);
}
From now on, every time you add or remove a ImageComment associated to Image, the number within
image_comment_count is adjusted automatically.
You can also specify counterScope. It allows you to specify a simple condition which tells the model
when to update (or when not to, depending on how you look at it) the counter value.
Using our Image model example, we can specify it like so:
class ImageComment extends AppModel {
public $belongsTo = array(
’Image’ => array(
’counterCache’ => true,
’counterScope’ => array(’Image.active’ => 1) // only count if "Image" is active )
);
}

 
 
 

pankaj

Skills    Cakephp

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

  Students (0)