Hi Chris:

In the simplest scenario, where the user is viewing one video at a
time:

- you have the Video id (passed as a parameter to the action, i.e.
http://www.example.com/app/videos/view/123)
- you have the User id ($this->user['id'] from your previous code
snippets)
- you can use the find() method on VideoFavorite using these id's
  - if the result set is not empty, the video is in the user's video
favorites, and you can tell the view to display the "Remove from
favorites"
  - if the result set is empty, the video is not in the user's video
favorites, so you need to tell the view to display the "Add to
favorites"

If the user is viewing more than one video at a time (i.e. you are
displaying the top 5 latest videos), it becomes more involved; but you
can extend this "algorithm".

For a more in-depth analysis and actual code recommendations, I will
need to charge by the hour. :-D

On Feb 9, 11:59 am, "[email protected]" <[email protected]> wrote:
> Hi Shadow,
> and again Thank you so much, but in some point it still unclear for me
> what to do exactly, I have tried it tried it,... and not giving up...!
> lol You sound like one of the Senior Programmer in here. I would like
> to ask you if you can take a look of my script,... I will give you
> access to ftp and phpmyadmin Please... Please... I have done so much
> to it, and can't give it up,... and Have a Million Dollar Idea to
> develop and implement to greatest Social site on the Net... LoLo But,
> that just me... Please help me if you can. You can grab the testing
> URL from my profile,... I don't wanna post it here, not to sound like
> a spam... And if you signup on my site, I will meet you there,... and
> there'll be my contacts, and skype info...
>
> Thank Yo Much
> chris
>
> On Feb 9, 6:44 am, ShadowCross <[email protected]> wrote:
>
>
>
> > 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).  
> > Seehttp://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. 
> > Seehttp://book.cakephp.org/view/1036/delete
> > andhttp://book.cakephp.org/view/1043/hasManyfordetails.
>
> > 3) Consider using CakePHP's counterCache option for your belongsTo
> > definitions.  Seehttp://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

Reply via email to