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 6187031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:59:29+00:00 2026-05-24T01:59:29+00:00

I’m using cakephp, I have a model comment with two fields : model_type and

  • 0

I’m using cakephp, I have a model “comment” with two fields : “model_type” and “model_id” in order to comment every item of my application (eg Picture, News, Article, …) with a single Comment model.

I wonder how to make this. (A component “Comment” for controller that could be commented ?)

Finally I want to list comment in view just with a helper:
$comment->show(‘model_name’, ‘item_id’);
that would display correctly paginated comments and a form for adding a new comment to the item.

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-24T01:59:30+00:00Added an answer on May 24, 2026 at 1:59 am

    Edit: See this too : http://bakery.cakephp.org/articles/AD7six/2008/03/13/polymorphic-behavior

    The Solution

    A multi model commenting system and pagination just with :

    <?php echo $this->element('comments', 
        array(
            'model_type' => "model_name", 
            'model_id' => $data['model']['id'],
            'order_field' => 'created',
            'order' => 'asc')); ?>
    

    The model

    Scheme :

    - id
    - model_type
    - model_id
    - content
    (optional:)
    - user_id
    - created
    - updated
    - rating
    - ...
    

    The Comment Model :

    
    // {app}/models/comment.php
    class Comment extends AppModel{
        public $name = 'Comment';
        
        // List of model that could be commented
        protected $model_list = array(
            'news' => array(
                'name' => 'News', // here it's the model's name
                'field' => 'validated'), // A field for validation ( allow comments only on validaded items)
            'articles' => array(
                'name' => 'Article',
                'field' => 'validated')
        );
        
            // This is an example
        public $belongsTo = array( 
            'User' => array(
                'conditions' => 'User.validated = true'
                )
            );
        
        public $validate = array(
            'model_type' => array(
                'rule' => 'checkModelType',
                'message' => "Something goes wrong !"
            ),
            'model_id' => array(
                'rule' => 'checkModelId',
                'message' => "Something goes wrong !"
            ),
            'content' => array(
                 'rule' => 'notEmpty',        
                 'message' => "Empty content !"
             )
        );
        
            // Check if the model is commentable
        public function checkModelType($data){
            $model_type = $data['model_type'];
            return in_array($model_type, array_keys($this->model_list));
        }
        
            // Check if the item exists and is validated
        public function checkModelId($data){
            $model_id = intval($data['model_id']);
            
            $model = $this->model_list[$this->data['Comment']['model_type']];
                    
            $params = array(
                'fields' => array('id', $model['field']),
                'conditions' => array(
                    $model['name'].'.'.$model['field'] => 1, // Validated item
                    $model['name'].'.id' => $model_id
                    )
            );
                
                    // Binding model to Comment Model since there is no $belongsTo
            return (bool) ClassRegistry::init($model['name'])->find('first', $params);
        }
    }
    

    The Controller

    
    // {app}/controllers/comments_controller.php
    class CommentsController extends AppController
    {
        public $name = 'Comments';
        
            // Pagination works fine !
        public $paginate = array(
            'limit' => 15,
            'order' => array(            
                'Comment.created' => 'asc')    
            );
        
        // The action that lists comments for a specific item (plus pagination and order !)
        public function view($model_type, $model_id, $order_field = 'created', $order = 'DESC'){
            $conditions = array(
                    'Comment.model_type' => $model_type, 
                    'Comment.model_id' => $model_id
                    );
            
                    // (optional)
            if($order_field != 'created') {
                $this->paginate['order'] = array(
                    'Comment.'.$order_field => $order,
                    'Comment.created' => 'asc');
            }
            
                    // Paginate comments
            $comments = $this->paginate($conditions);
                    
                    // This allow to use paginator with requestAction
            $paginator = ClassRegistry::getObject('view')->loaded['paginator'];
            $paginator->params = $this->params;
            return compact('comments', 'paginator');
        }
    
        public function add(){
            // What you want !
        }
    }
    
    

    The views

    Comments element

    /* {app}/views/elements/comments.ctp
    @params : $model_type
    *   : $model_id
    * 
    * */
    
    $result = $this->requestAction("/comments/view/$model_type/$model_id/$order_field/$order/", $this->passedArgs);
    $paginator = $result['paginator'];
    $comments = $result['comments'];
    
    $paginator->options(array('url' => $this->passedArgs));
    ?>
    <h2>Comments</h2>
    
    <fieldset class="commentform">
    <legend>Add un commentaire</legend>
    <?php
        // Form
        echo $form->create('Comment', array('action' => 'add'));
        echo $form->hidden('model_type', array('value' => $model_type));
        echo $form->hidden('model_id', array('value' => $model_id));
        echo $form->input('content');
    <?php
        echo $form->end('Send');
    ?>
    </fieldset>
    <div class="paginationBar">
        <?php
            echo $paginator->prev('<<  ', null, null, array('class' => 'disabled'));
            echo '<span class="pagination">',$paginator->numbers(),'</span>';
            echo $paginator->next('  >>', null, null, array('class' => 'disabled'));
        ?>
    </div>
    <?php
        foreach($comments as $comment){
            echo $this->element('comment', array('comment' => $comment));
        }
    ?>
    <div class="paginationBar">
        <?php
            echo $paginator->prev('<<  ', null, null, array('class' => 'disabled'));
            echo '<span class="pagination">',$paginator->numbers(),'</span>';
            echo $paginator->next('  >>', null, null, array('class' => 'disabled'));
        ?>
        
        <p><br />
        <?php
            echo $paginator->counter(array('format' => 'Page %page% on %pages%, displayi  %current% items of %count%'));
        ?>
        </p>
    </div>
    

    Comment element

    //{app}/views/elements/comment.ctp
    // A single comment view
    $id = $comment['Comment']['id'];
    ?>
    <div class="comment">
    <p class="com-author">
    <span class="com-authorname"><?=$comment['User']['name']?>&nbsp;</span>
    <span class="com-date">(<?=$comment['Comment']['created']?>)</span>
    </p>
    <p class="com-content"><?=$comment['Comment']['content']?></p>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

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.