CakePHP: Creating a New HABTM Line Instead of Updates

I have two models with the HABTM ratio (has and belongs to many): Qsets and Questions.

The next action (in QsetsController.php) should lead to a new row in the qsets_questions table, in which a new question will appear in the new qset. But instead, it updates the existing rows, as a result of which this question begins with the previous qset and is added to the new one.

What am I doing wrong?

public function admin_add_question( $qset_id, $question_id) {

    //find the qset...
    $qset = $this->Qset->find('first', array('id'=>$qset_id));

    $this->Qset->QsetsQuestion->create();
    $data = array(
            "Qset"=> array ("id"=>$qset_id),
            "Question"=>array ("id"=>$question_id)
    );
    Controller::loadModel('Question');
    $r= $this->Question->save($data);

    $this->Session->setFlash('Question id['.$question_id.'] added.');

    $this->redirect( $this->referer() );
}

If this is not clear from my description, this happens:

Before adding a question ...

**Qset 1**
Question 1
Question 2

**Qset 2**
Question 3
Question 4

What should happen when adding Question 2 to Qset 2

**Qset 1**
Question 1
Question 2

**Qset 2**
Question 3
Question 4
Question 2

What happens instead ...

**Qset 1**
Question 1
          <----removed
**Qset 2**
Question 3
Question 4
Question 2

Update : Here's a dump of the qsets_questions table:

CREATE TABLE `qsets_questions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `question_id` int(11) NOT NULL,
  `qset_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=64 ;

Decision:

Here's the working version, thanks @nuns

public function admin_add_question( $qset_id, $question_id) {

    $this->Qset->QsetsQuestion->create();
    $data = array(
            "qset_id"=>$qset_id,
            "question_id"=>$question_id
    );
    Controller::loadModel('Question');
    $this->Question->QsetsQuestion->save($data);
    $this->Session->setFlash('Question id['.$question_id.'] added.');       
    $this->redirect( $this->referer() );
}
+3
1

, :

HABTM. , , , , , primaryKeys displayFields ..

, HABTM. , , a Question Qset . ,

$data = array('Question'=>array('title'=>'new_question'),
              'Qset'=>array('name'=>'lets say qset'));
$this->Question->saveAll($data);

, cake HABTM , . QsetsQuestion . , , , , :

$data = array('qset_id'=> $qset_id,
              'question_id'=> $question_id);
$this->Question->QsetsQuestion->save($data);

qsets_questions, , .

, , , , , , .

[EDIT] "", .

Cakephp

HasAndBelongsToMany Cake , . , 10 . 2 . 2 , 12.

, , . - Qsets, , $data ( , ). HABTM ( "Challenge IV" ).

, , , "", , QsetsQuestion. , , Questions . QsetsQuestion :

Qset hasMany QsetsQuestion
QsetsQuestion belongsTo Qset, Question
Question hasMany Qsets.

... , .

, :

  • , Qset-Question, , , ,

    //find previously associated Qsets, lets say it 1,2,3 and 4
    $data = array('Question'=>array('id'=>1),
              'Qsets'=>array('Qsets'=>array(1,2,3,4, $new_qset));
    $this->Question-save($data);
    

, QsetsQuestion, . , HABTM .

  • QsetsQuestion , .

    $data = array('qset_id'=>1, 'question_id'=>1)
    $this->Question->QsetsQuestion->save($data);    //also works with $this->Qset->QsetsQuestion
    

(), , (, 2-2 ). .

  • cakephp ... .
+3

Source: https://habr.com/ru/post/1532144/


All Articles