drfqfuhej48511519 2012-12-18 19:18
浏览 6

插入/更新蛋糕php

Hello All im stuck in a weird position.

I have a table with the columns. id_acces, id_user, id_menu, has_access (boolean)

im basically have a form with a list of checkboxes and id numbers to allow access to parts of a page to a specific user. Basically i need to update the table with either has access or not and if there is no entry for the user regarding to pages chosen to create a new entry in the table. So my array im getting back looks like this

     Array
       (
        [AdminMenu] => Array
          (
            [id_user] => 1
            [id_menu] => Array
            (
                [4] => enabled
                [21] => enabled
                [22] => enabled
                [23] => enabled
                [24] => enabled
                [1] => enabled
                [5] => enabled
                [25] => enabled
                [26] => enabled
                [6] => enabled
                [7] => enabled
                [29] => enabled
                [8] => enabled
                [30] => enabled
                [31] => enabled
                [32] => enabled
                [33] => enabled
                [34] => enabled
                [35] => enabled
                [36] => enabled
                [37] => enabled
                [38] => enabled
                [39] => enabled
                [40] => enabled
                [41] => enabled
                [60] => enabled
                [2] => enabled
                [10] => enabled
                [11] => enabled
                [12] => enabled
                [13] => enabled
                [14] => enabled
                [15] => enabled
                [16] => enabled
                [17] => enabled
                [18] => enabled
                [9] => enabled
                [3] => enabled
                [19] => enabled
                [20] => enabled
            )

           )

    )

The key is the id_menu and the value is just if the checkbox is checked or not.

can someone please tell me how i can user this array to do a insert or update in cake php to my table. Im currently looping through the array as $key=>$value and running a query against the table for the key to exist. if it exists then i need to run a update query if not then i need to do a insert query for that specific page or id_menu. Please help

    foreach($this->data['AdminMenu']['id_menu'] as $key=>$value) {
          $check = select against the table where the id_menu = $key
       if($key = $check[0]['AdminAccess']['id_menu']) {
             $this->AdminAcess->id_menu=$key;
     $this->AdminAccess->id_user=$user_id;
     $this->AdminAccess->set('has_access', 't');
     $this->AdminAccess->save();
        } else {
           $this->AdminAcess->id_menu=$key;
    $this->AdminAccess->id_user=$user_id;
    $this->AdminAccess->saveField('has_access', 'false');

         }

    }

im sure there is got to be a easier way then this loop. Also how do i do the insert or update. this isnt even doing a insert or update either way

  • 写回答

1条回答 默认 最新

  • drb56625 2012-12-20 04:04
    关注

    This is a hasAndBelongsToMany relationship. You should read through this section of the Cake Book, and adapt your form code to follow their suggestions:

    http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-habtm

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题