Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:54:28+00:00 2026-05-31T12:54:28+00:00

So I wrote a flag feature for flagging comments, and I’m checking in my

  • 0

So I wrote a flag feature for flagging comments, and I’m checking in my controller to see if the user has already flagged a particular comment. The problem is, I think this can be done in the Model.

The function is:

private function userAlreadyFlagged($userId, $commentId) {
    $userAlreadyFlagged = $this->CommentsFlag->find('count', array(
        'conditions' => array('CommentsFlag.comment_id' => $commentId, 'CommentsFlag.user_id' => $userId)
            ));

    if ($userAlreadyFlagged > 0) {
        return true;
    } else {
        return false;
    }
}

Then I have another function also within the controller called flagComment

if (!$this->userAlreadyFlagged($userId, $commentId)) {
    if ($this->CommentsFlag->save($this->request->data)) {
        $message = array('response' => 'success');
    }
} else {
    $message = array('response' => 'alreadyFlagged');
}​

My model for the CommentsFlag current links my users table to it:

var $name = 'CommentsFlag';
var $belongsTo = array(
    'User' => array(
        'className' => 'User'
    )
);

So I’m wondering how I can start doing stuff like this in the model from now on, because I have a lot of code like this that would be better suited for the Model.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:54:29+00:00Added an answer on May 31, 2026 at 12:54 pm

    It is a good practice to have fat-models. What problem are you facing in moving the above functions to model?

    Copy those functions into your model (CommentsFlag) and replace all $this->CommentsFlag->find(... with $this->find(

    Consequently, to call above functions from your model, you can use:

    $this->CommentsFlag->userAlreadyFlagged() if called from CommentsFlag’s controller

    or
    $this->User->CommentsFlag->userAlreadyFlagged() if called from User’s controller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a programm which scans BCL types and checks beforefieldinit flag. So some
I wrote a small program which frequently opens small, user text files and until
I am new to visual C++. I wrote my class, but it has error
I wrote an android program that: has a main activity for UI, and it
I wrote a windows service use the Event Log for logging. It has wrote
I just wrote up this test to see if I was crazy... using System;
I wrote an AppWidget that has a configuration activity(I used the same configuration activity
I have a table that stores various types of flags. Each flag type has
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
I wrote a custom URL rewriting module, to take certain paths and map them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.