0

我们有两个模型,它们通过具有和属于许多(HABTM)关系相关:工作,测试。我们能够成功添加/编辑关系(我们知道是因为它们出现在连接表中),但它们的创建和修改字段从未设置。

以下是模型关系:

//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'
            )

    );

这是/view/Jobs/edit.ctp

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

这是我们在控制器中更新的方式...

    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');
        }
    } 

连接表中的记录是正确的,但创建和修改的始终为 NULL。

我们做错了什么?

4

1 回答 1

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.

于 2013-11-08T08:42:00.427 回答