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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:25:20+00:00 2026-06-14T18:25:20+00:00

When I try to add some entries into the join table of two classes,

  • 0

When I try to add some entries into the join table of two classes, I run into issues with CakePHP.

Here is my join table:

posts_comments

id
post_id
comment_id

my post Model:

<?php

  class Post extends AppModel {
    var $hasManyAndBelongsTo = array('Comment')
  }

?>

My Comment Model:

<?php

  class Commentextends AppModel {

    var $hasAndBelongsToMany = array('Post');

  }

?>

My Controller:

ForumController:

function save() {
    if ($this->data) {
        $this->loadModel('PostComment');
        $this->layout = null;
        debug($this->data);
        $this->PostComment->save($this->data);
    }
    $this->redirect(array('action' => 'view', $this->data['PostComment']['PostId']));
}

And my view.ctp:

<?php 
echo $this->Form->create('PostComment', array('url' => 'save', 'id' => 'save')); ?>
echo $this->Form->hidden('PostComment.PostId', array('value' => $id));
echo $this->Form->input('PostComment.CommentId', array('label' => '', 'type' => 'select', 'multiple' => 'checkbox', 'options' => $CommentName, 'selected' => $CommentId));
echo $this->Form->submit('save', array('id' => 'submit'));
echo $this->Form->end();
?>

When I click on submit, nothing is submitted, and the redirection happens, but the data contained in my $this->data is not save in my BDD.

But what I want is that when I submit my form, I don’t want that CakePHP create a new post or a new comment, I just want a new relation between the two.

I already succeed to done it, but I used

$this->myObject->query("INSERT INTO blablabla")

The problem is that I have a multiple checkboxes, so I don’t really know how I can easily check that one box has been unchecked or checked.

  • 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-06-14T18:25:21+00:00Added an answer on June 14, 2026 at 6:25 pm

    You can work in a simpler way if you create another model with two hasMany relations instead of one with HABTM.

    HasAndBelongsToMany between two models is in reality shorthand for
    three models associated through both a hasMany and a belongsTo
    association.

    I found it is one of the simplest ways to save and retrieve data.

    For more information: http://book.cakephp.org/2.0/en/models/saving-your-data.html#what-to-do-when-habtm-becomes-complicated

    At the new model, you could do something like:

    public addRecord($userId, $postId)
        $this->data[$this->alias]['user_id'] = $userId;
        $this->data[$this->alias]['post_id'] = $postId;
    
        return $this->save($this->data[$this->alias]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to do static class, add to icollection but i got some issues
I try to add some C/C++ sources into JNI folder by dragging from explorer
I declared a Dictionary type object and try to add some items into it.
I am digging into Twitter's Bootstrap and now want to try and add some
I try add to some table new column, how its described in documentation for
I am attempting to add some entities that I have created. When I try
When I try to add struct in two-dimensional array cin>>array[1][items.stoka_ime]; in error list write
I try to add some css attributes to labels in my custom sfForm but
So, I know some Python but I thought that I should try to add
I am editing a template to try and add some conditional logic to my

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.