doujiang1913 2010-10-05 16:35
浏览 48
已采纳

Kohana 2.3.4 ORM - 删除数据透视表关系

I'm trying to remove relationships from a pivot table with ORM's remove method. This is for an edit method that updates the categories associated with a product. I can successfully add multiple relationships, but I need to remove those relationships prior to adding them again.

Here's how I add them

           foreach ($categories as $addCat)
              {
                $product->add(ORM::factory('category', $addCat));
              }

$categories is an array of items from a form and $product is the model. This works perfect.

I think I need to do something like this to remove them, but it's not working

$product->remove(ORM::factory('category', $product->id));

$product->id is the id of a product. I am calling this script before the add script. My goal is remove all the relationships that contain the value in "$product->id" and then run my loop to add them again. I could do this easily if I created a model for the pivot table, but that would go against the point of using pivot tables.

  • 写回答

1条回答 默认 最新

  • dongnuo3749 2010-10-05 19:52
    关注

    You have the right idea. When you call ORM::factory('category', $product->id) you are trying to find a category with the same id as your product. Instead you need to specify the id of the category you want to remove.

    Say you have an array of id's of categories that you want to remove:

    $categories = array('2', '4', '6');
    foreach ($categories as $cat_id)
    {
        $product->remove(ORM::factory('category', $cat_id));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案