Chris: After reviewing your code snippets, here are some of my recommendations:
1) Instead of defining your classes as: Video - belongsTo VideoCategory - belongsTo User - hasMany VideoComment - hasMany VideoFavorite VideoFavorite - belongsTo User - belongsTo Video User - hasMany Video - hasMany VideoFavorite consider the following instead: Video - belongsTo VideoCategory - belongsTo User - hasMany VideoComment - hasAndBelongsToMany User with VideoFavorite (alias: VideoFavorite1) VideoFavorite - belongsTo User - belongsTo Video User - hasMany Video - hasAndBelongsToMany Video with VideoFavorite (alias: VideoFavorite2) Note that Video has two relations to User, and vice-versa; the "alias" just needs tbe different (I used arbitrary ones). See http://book.cakephp.org/view/1046/Multiple-relations-to-the-same-model You can the do something like the following: controllers/videos: function favorites() { $this->User->contain('VideoFavorite2'); $info = $this->User->find('first', array( 'conditions' => array('User.id' => $this->user['id']) ); ... } The data returned ($info) will include a sub-array (with the index VideoFavorite2) of all the Video rows tagged as that user's favorite. You can then use that sub-array to determine whether a particular video should have the "Add to favorites" or the "Remove from favorites" (i.e. in_array() or Set::contains(), or your favorite method of determining whether one array contained in another array). 1a) Whether you follow this recommendation or not, the answer to your question is basically: - Get the list of the user's favorite videos - Check if the particular video is in the list. - If yes, display the "Remove from favorites" - If no, display the "Add to favorites" 1b) the video_user_id field in the video_favorites table seems redundant. VideoFavorite belongsTo Video, so that information can easily be obtained directly from Video, without any significant loss in performance (i.e. $res = $this->VideoFavorite->find('all', array( 'contain' => 'Video' )); and using $res['Video']['user_id'] instead of $res['VideoFavorite'] ['video_user_id'] ) 2) Take advantage of CakePHP's cascading delete. Basically, if you include 'dependent' => true as part of your Video hasMany VideoComment and hasMany VideoFavorite definition, the code in your Video.afterDelete() method is unnecessary. See http://book.cakephp.org/view/1036/delete and http://book.cakephp.org/view/1043/hasMany for details. 3) Consider using CakePHP's counterCache option for your belongsTo definitions. See http://book.cakephp.org/view/1042/belongsTo . Advantages: a) reduce manual queries. Currently, your set_favorites() method hard- codes the fociki_users tablename. What if you decide to change the tablePrefix later? b) promotes the thin controller / fat model concept (where most of the business logic is defined in the model, and the controller only links the model with the view). What happens if you later want to add the option to automatically add all videos uploaded by the user's favorite author? If you leave the "update query" in the controller, you will need to add the same code to the new controller/action. ===== To summarize my recommendations: take the time to design your model relations and let the framework do most of the work; that's the advantage of using a framework -- it facilitates the "rudimentary" tasks so you can focus on your business logic. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php
