HABTM form validation in CakePHP

前端 未结 7 1386
我在风中等你
我在风中等你 2020-12-03 06:33

I have a Projects table and a Users table which are linked by a HABTM relation. In the \"add\" new Project page I have a multiple checkbox section to select Users for the ne

7条回答
  •  心在旅途
    2020-12-03 07:02

    If you are using CakePHP 2.3.x, you may need to add this code to your model in addition to the code that GuidoH provided, otherwise your HABTM model data may not save:

    public function beforeSave($options = array()){
      foreach (array_keys($this->hasAndBelongsToMany) as $model){
        if(isset($this->data[$this->name][$model])){
          $this->data[$model][$model] = $this->data[$this->name][$model];
          unset($this->data[$this->name][$model]);
        }
      }
      return true;
    }
    

提交回复
热议问题