0

We have two models which are related by a has and belongs to many (HABTM) relationship: Jobs, Tests. We are able to add/edit the relationships successfully (we know because they show up in the join table), but their created and modified fields are never set.

Here are the model relationships:

//Job.php
public $hasAndBelongsToMany = array (
    'Test' => array (
        'classname' => 'Test', 
        'foreignKey'=>'job_id',
        'joinTable' => 'jobs_tests',
        'associatedForeignKey' => 'test_id'
    )
);

//Test.php
    public $hasAndBelongsToMany = array(
        'Job' => array(
            'className'=> 'Job',
            'joinTable'=>'jobs_tests',
            'foreignKey' => 'test_id',
            'associatedForeignKey'=> 'job_id'
            )

    );

Here is the /view/Jobs/edit.ctp

            echo $this->Form->select('Test', $test_options, array('class'=>'form-control', 'multiple'=>'checkbox'));
//This is always empty (nothing selected/checked). 

Here is how we update in the controller...

    if ($this->request->isPut() ) {
        $data = $this->request->data;
        $save = $this->Job->save( $data );
        if ($save) {
            $this->Session->setFlash('Job edited');
        } else {
            $this->Session->setFlash('Error editing job');
        }
    } 

The records in the join table are correct but created and modified are always NULL.

What are we doing wrong?

emersonthis
  • 32,822
  • 59
  • 210
  • 375

1 Answers1

0

I dont't think that cake fills created and modified even in HABTM join table. (I tried in one project of mine and it didn't work too but maybe it can be activated somwhow)

However if you don't set unique to true in your association array the records are deleted and re-created on every update, so the update field has no meaning here.

If you really want the two fields then you have to use the Has Many Trough relationship.

arilia
  • 9,373
  • 2
  • 20
  • 44