php - How to test if a category already exists? PrestaShop
问题描述
I get the category from other database web service and I put them on PrestaShop when I refresh the file to add categories I wanna make sure if the category id exists, if exist I wanna update the category.
$XMLRQString = '<?xml version="1.0" encoding="utf-8"?>'.
'<x:Winmax4GetFamiliesRQ xmlns:x="urn:Winmax4GetFamiliesRQ">'.
'</x:Winmax4GetFamiliesRQ >';
$return = $client->GetFamilies($Params);
$XMLRSString = new SimpleXMLElement($return->GetFamiliesResult);
if ($XMLRSString->Code > 0)
echo '</br>Error: '.$XMLRSString->Code." ".$XMLRSString->Message;
else{
foreach ($XMLRSString->Families->Family as $family)
{
$category = new Category();
$category->id = $family->Code;
$category->force_id = true;
$category->is_root_category = false;
$category->name = array((int)Configuration::get('PS_LANG_DEFAULT') => $family->Designation);
$category->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => $family->Code);
$category->id_parent = Configuration::get('PS_HOME_CATEGORY');
$category->add();
}
}
解决方案
一般来说,如果您有大数据(还有安全数据),最好将数据直接插入数据库。使用 Db 类而不是 Category 类。
否则,您必须使用 save() 方法而不是 add() 方法
推荐阅读
- c++ - 与朋友 c++ 打交道时,相同的代码,不同的结果
- git - git mv 文件夹并随后将新文件从功能分支合并到修改后的路径,是否会删除其历史记录?
- mysql - 如何在 sequelize 中的查询中填充外键(就像在 mongoose 中完成的那样)(nodejs)
- .net - 将 .mdb 保存在共享驱动器中的 commonFile 上以进行多用户连接
- javascript - voiceChannel.join() 不是函数节点 v16 discord.js
- java - iText 7 - 在标题单元格中旋转文本
- c - 如何取消引用 inet_ntop 的双指针
- visual-studio - 通过 Shell 运行时,Visual Studio 编译器不退出(返回值 STILL_ACTIVE)
- ios - WKWebView - 设备旋转时内容不旋转
- excel - 如果右侧的单元格中有文本,则将公式插入单元格