douzong3599 2013-11-07 18:38
浏览 36

CakePHP:创建和修改的字段未在连接表中更新

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?

  • 写回答

1条回答 默认 最新

  • duankong8998 2013-11-08 08:42
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看