[英]insert / update in cake 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) id_acces,id_user,id_menu,has_access(布尔值)
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. 我基本上有一个带有复选框和ID号列表的表单,以允许特定用户访问页面的各个部分。 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. 关键是id_menu,值就是是否选中该复选框。
can someone please tell me how i can user this array to do a insert or update in cake php to my table. 有人可以告诉我如何使用此数组在cake php中插入或更新到我的表。 Im currently looping through the array as $key=>$value and running a query against the table for the key to exist. 我目前以$ key => $ value的形式遍历数组,并针对该表运行查询以查询该键是否存在。 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. 如果存在,那么我需要运行一个更新查询,如果不存在,那么我需要对该特定页面或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 这甚至都没有进行插入或更新的方式
This is a hasAndBelongsToMany relationship. 这是一个hasAndBelongsToMany关系。 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 http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-habtm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.