A simpler way to do this would be to set a variable to the view. So,
you wouldn't need to call the controller every time. Rather, in the
controller where you setup the view do the following
function showPost($id) {
$post = $this->Post->findById($id);
$videos = $this->Post->Postvideo-
>find('first',array('conditions'=>array('Postvideo.post_id'=>$id)));
$this->set('post', $post)
$this->set('hasVideo', !empty($videos));
}
This will return your single post along with a boolean to tell you if
you have a video. This can scale to multiple posts easily enough if
you use recursive=0, and then check if there's a Videopost item
returned as part of the post.
Hope that makes sense.
On Mar 5, 12:57 am, Evangelos <dia...@gmail.com> wrote:
> Hi,
>
> I have a posts table and a postvideos table. Posts can have a main
> video but can also have many videos. In my view I want to display a
> video icon next to the post title when a post has a main or many
> videos.
>
> so at the moment I have a function at the controller that checks
> whether a post has a main or many videos and returns a video image
> icon, which I display in my view with requestAction. However I have a
> strong feeling that this is a wrong way of doing things and it breaks
> MVC. What is the recommended approach for something like this?
>
> Controller:
>
> function hasvideo($id) {
> App::import('Helper', 'Html');
> $html = new HtmlHelper();
> $post = $this->Post->findById($id);
> $videos = count($post['Postvideo']); if($post['Post']
> ['main_video'] && $post['Post']['media'] == 1) $hasmainvideo = 1; else
> $hasmainvideo = 0;
> if($videos > 0 || $hasmainvideo == 1) return $html->image('themed/kretaworldbasket/video.png', array('title' => 'Post has
>
> video', 'width' => 16, 'height' => 8, 'style' => 'margin-left:
> 5px;'));
>
> }
>
> View:
> <?php echo $this->requestAction('/posts/hasvideo/'.$editorpick['Post']
> ['id']); ?>
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment