首页 文章

保存许多现有记录和一条新记录之间的关系

提问于
浏览
1

使用CakePHP,我有活动和人 . 我想在保存新事件时将许多现有人分配给我正在创建的事件 . 我正在使用复选框选择人员 .

我的数据如下:

data[Event][name]:Test 4
data[Event][event_type_id]:1
data[EventPerson][person_id]:
data[EventPerson][person_id][]:65
data[EventPerson][person_id][]:72
data[EventPerson][person_id][]:67

我有 EventPerson 模型,彼此之间设置了HABTM关系,以及 events_people 表和模型,并且这些模型已正确设置 .

我试过 $this->Event->saveAssociated$this->Event->saveAll . 每个人都会用正确的信息保存事件记录,也不会保存与人的关系 .

保存事件时,人们将始终存在 . 我觉得我几个小时都在wra my brain with with with with with with with with with with with . . . . .

Tried

调节器

$this->Event->create();
    if ($this->Event->save($this->request->data)) {
        $this->Session->setFlash(__('The event has been saved'));
        return $this->redirect...
    }
$people= $this->FestivalPerson->find('all', 
    array(
        "Person.name",
        "conditions" => array(
            "festival_id" => $this->request->query["festival_id"]
        ), 
        "order" => "Person.name ASC"
    )
);
$this->set("eventPeople", Set::combine($people, '{n}.Person.id', '{n}.Person.name'));

视图:

echo $this->Form->input('EventPerson', array('multiple' => 'checkbox'));

数据:

data[Event][EventPerson]:
data[Event][EventPerson][]:50
data[Event][EventPerson][]:65
data[Event][EventPerson][]:71

看起来它与每个示例的结构相匹配,应该保存得当,但我的 events_people 表仍然是空的 . 完全没有来自PHP / MySQL的错误 .

1 回答

  • 1

    像这样形成您的数据

    data[Event][name]:Test 4
    data[Event][event_type_id]:1
    data[EventPerson][0][person_id]:
    data[EventPerson][1][person_id][]:65
    data[EventPerson][2][person_id][]:72
    data[EventPerson][2][person_id][]:67
    

    或者关注这篇文章:http://patisserie.keensoftware.com/en/pages/how-to-save-habtm-data-in-cakephp

    它解释了如何在cakePhp中编写数据数组的自定义代码,我相信那里有一个类似的例子

相关问题