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

  • Home
  • SEARCH
  • 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 7601503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:10:45+00:00 2026-05-30T23:10:45+00:00

I have a Comment Model that I am using to store comments for both

  • 0

I have a Comment Model that I am using to store comments for both Goals and Notes.

The current implementation has a GoalComment Model and a NoteComment Model, each of which extends Comment, and the Comment‘s beforeFind does the following:

$queryData['conditions'][$this->name . '.' . $this->__type_field] = $this->name;

which basically sets the type field of the Comment Model to either GoalComment or NoteComment, depending on which of those classes is performing a find.

This implementation has worked up until now, where I want to delete comments. Below is the Model association for Comments in Goals:

var $hasMany = array(
    'Comment' => array(
        'className' => 'GoalComment',
        'foreignKey' => 'object_id'
    )
);

And in my beforeDelete, where I want to remove all comments associated with the goal, I have:

$this->Comment->deleteAll(array('object_id' => $this->id));

But I get the following SQL error:

SQL Error: 1054: Unknown column 'GoalComment.type' in 'where clause' [CORE\cake\libs\model\datasources\dbo_source.php, line 525]
Query: SELECT `Comment`.`id` FROM `comments` AS `Comment` LEFT JOIN `users` AS `Poster` ON (`Comment`.`poster_id` = `Poster`.`id`)
LEFT JOIN `goals` AS `Goal` ON (`Comment`.`object_id` = `Goal`.`id`)  
WHERE `object_id` = 52 AND `GoalComment`.`type` = 'GoalComment'

This is happening because of the beforeFind action I described before, which adds the GoalComment.type = 'GoalComment'. This has made me rethink the whole approach to the situation.

So my question is how should this relationship be re-implemented? My best idea is to scrap the beforeFind in Comment and simply remake the $hasMany relationship to be:

var $hasMany = array(
    'Comment' => array(
        'className' => 'Comment',
        'foreignKey' => 'object_id',
        'conditions' => array('Comment.type = "GoalComment"')
    )
);

This would extract all of the thinking from the Comment Model, and it just seems more natural to me.

Do you have an idea for a better implementation, or should I go with the one above? Thanks!

  • 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-30T23:10:47+00:00Added an answer on May 30, 2026 at 11:10 pm

    I think you’re correct in realizing that you’ve been over-thinking the associations.

    The “normal” way for this type of relationship is to simply use the fields model and foreign_key in your comments table.

    //Comment model
    public $belongsTo = array(
        'Goal' => array(
            'className' => 'Goal',
            'foreignKey' => 'foreign_key',
            'conditions' => array('Comment.model' => 'Goal')
        ,
        'Note' => array(
            'className' => 'Note',
            'foreignKey' => 'foreign_key',
            'conditions' => array('Comment.model' => 'Note')
        )
    );
    
    //Goal model
    public $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
            'foreignKey' => 'foreign_key',
            'conditions' => array('Comment.model' => 'Goal')
        )
    );
    
    //Note model
    public $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
            'foreignKey' => 'foreign_key',
            'conditions' => array('Comment.model' => 'Note')
        )
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Entity Framework, and I have a COMMENT entity. A COMMENT has a
I have two models, Article and Post that both inherit from a base model
I'm using cakephp, I have a model comment with two fields : model_type and
I have a model that looks something like this: class Comment < ActiveRecord::Base ...
I have a Comment model in my app but am encountering a lot of
I have Comment as a polymorphic model. It is attached to Post, Review, etc.
I have a YML file containing fixtures for a Rails model (Comment) which looks
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
Assuming I have a comments model and a posts model, What code can I
Okay i have two models: posts and comments. as you can think comments has

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.