| Новости | FAQ | Авторы | Документация | В действии | Библиотека |
| Инструменты | Полезные ссылки | Хостинги | Скачать | Примеры | Форум |
Mizter Egoist 08.06.2017 18:39
У страницы есть атрибуты (свойства), каждый атрибут записывается в БД отдельной строкой.@_updateAttr[eID;data]
^if(def $eID){
$_attr[^get.query[
$.table[attributes]
$.type[hash]
$.select[
$.eID[= '$eID']
]
]]
$attr[^get.selectData[$_attr]]
^if(def $attr){
$attrTable[^table::create{id img name type link attr}]
^data.foreach[id;item]{
^attrTable.append[
$.id[$id]
$.img[^if(def $item.img){$item.img}{}]
$.name[^if(def $item.name){$item.name}{}]
$.type[^if(def $item.type){$item.type}{}]
$.link[^if(def $item.link){$item.link}{}]
$.attr[^if(def $item.arr){[^item.arr.foreach[;values]{^json:string[$values]}[, ]]}{}]
]
}
$attrHash[^attrTable.hash[id]]
^attrTable.menu{
$attrExist[$attr.[$attrTable.id]]
^if($attrExist){
^if($attrExist.name ne $attrTable.name){
Update name
}($attrExist.type ne $attrTable.type){
Update type
}($attrExist.attr ne $attrTable.attr){
Update attr
}
}{
^rem{
#
Insert new
#
}
^_insertAttr[$eID;$attrHash.[$attrTable.id]]
}
}
}
}
@_insertAttr[eID;data]
^if(def $eID){
^json:string[$data]
}Как обновлять атрибут мне ясно, но вот как добавить новый не понятно, перепробовал разные способы foreach, не один не работает как нужно.{
"id":"3d4c21b3",
"img":"",
"name":"Name",
"type":"1",
"link":"",
"attr":"[{"name":"Value", "value":"5"}, {"name":"Value", "value":"6"}]"
}
{
"id":"0314e171",
"img":"",
"name":"Jane",
"type":"3",
"link":"",
"attr":"[{"name":"Value", "value":"7"}, {"name":"Value", "value":"8"}]"
}Этот хеш нужно перебрать, убрать из него id и добавить eID, как в Вашей конструкции: INSERT INTO attributes
(eID, img, name, type, link, attr)
VALUES
^data.foreach[;item]{(
'$eID',
'^if(def $item.img){$item.img}{}',
'^if(def $item.name){$item.name}{}',
'^if(def $item.type){$item.type}{}',
'^if(def $item.link){$item.link}{}',
'^if(def $item.attr){$item.attr}{}'
)}[, ]Вот только она не работает.