【问题标题】:CakePHP HABTM relationship issueCakePHP HABTM 关系问题
【发布时间】:2012-05-25 03:25:44
【问题描述】:

我正在尝试创建一种社交网络类型的网站,用户可以在其中发送/接收好友请求。在我们当前的系统中,当用户发送请求时,所需信息的一半存储在数据库中,然后当用户接受其余信息时,其余信息将被输入。

relationships_users 表是 id、partyone、partytwo、expirydate、active

我遇到的问题是,一旦用户接受请求并输入到期日期,它就会插入到新列中 - 而不是与该用户相关的列中

here is my add function

    if($this->request->is('post')){
    $this->Relationship->create(); 
    if ($this->Relationship->save($this->request->data)) 
    { 
        $this->Session->setFlash('The relationship has been saved');  

    }
    else { $this->Session->setFlash('The relationship could not be saved. Please, try again.'); } 
    } 

  }

here is my approve function

if($this->request->is('post')){
        $this->Relationship->create(); 
        $this->Relationship->save($this->request->data);
        $this->Session->setFlash('The information has been saved');}
        else{$this->Session->setFlash('The information couldnt be saved');}

我假设我错过了一些简单的代码,任何帮助将不胜感激 它插入到新列中 - 不在与该用户关系相关的列中*

【问题讨论】:

    标签: cakephp has-and-belongs-to-many relationships


    【解决方案1】:

    您的帖子完全没有问题,但据我了解,您应该遵循以下步骤:

    • 当信息的前半部分保存在Relationship 表中时,您必须获取该条目的id,您可以获得如下信息:

      $this->Relationship->save($this->request->data);

      $lastId = $this->Relationship->id; // will gives you id of first half info saved

    • 然后您必须将 $lastId 与其余信息(即请求确认后的后半部分)一起嵌入。如果您可以将该 ID 嵌入到 $this->request->data 内的后半部分信息中,那么 $this->Relationship->save($this->request->data) 将更新之前保存的行。在这种情况下,您不需要$this->Relationship->create();

      根据 cakePHP,如果任何数据中包含id,那么它不会创建新行并保存,而是更新具有id 的相应行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多