0
votes

I'm using CakePHP 2 and I have two models: Questions & Topics which are connected via a HABTM relationship. In my 'add' form for questions, I have a variable amount of autocomplete fields which should serve to couple (existing) topics to the Question the user is adding.

What I want is: When I save my Question, that the relations with the selected topics are saved automatically as well. Can someone show me how I can achieve this? Preferably I'd like for cake, by naming my form helper fields correctly, to handle as much of this automatically without me having to perform manual updates in the controller. Is this possible?

Would it look something like this:

<?php echo $this->Form->input('RelatedTopic.0.topic_id'); ?>
<?php echo $this->Form->input('RelatedTopic.1.topic_id'); ?>
..

This is my relation in the Question model:

$hasAndBelongsToMany= array(
  'RelatedTopic' => array(
    'className' => 'Topic',
    'joinTable' => 'questions_topic',
    'foreignKey' => 'question_id',
    'associationForeignKey' => 'topic_id',
  )
);
1

1 Answers

0
votes

Are you adding new topics related to the question or do you have a predetermined list of topics for the questions?