我想定义我自己的Category id,比如(999,2222,3333)并强制在PrestaShop数据库上使用它,但是当我执行它时(1,2,3),我如何才能强制使用我自己的id
foreach ($XMLRSString->Families->Family as $family)
{
$_GET['forceIDs'] = true;
$category = new Category($family->Code);
$category->id = array((int)Configuration::get('PS_LANG_DEFAULT') => $family->Code);
$category->id_category = array((int)Configuration::get('PS_LANG_DEFAULT') => $family->Code);
$category->id_category_default = array((int)Configuration::get('PS_LANG_DEFAULT') => $family->Code);
$category->is_root_category = false;
$category->name = array((int)Configuration::get('PS_LANG_DEFAULT') => $family->Designation);
$category->id_parent = Configuration::get('PS_HOME_CATEGORY');
$category->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => $family->Code);
$category->add();
}
发布于 2019-05-28 02:10:56
在PrestaShop中,如果您想强制指定特定对象(类别、产品、订单等)的ID。要在数据库插入期间绕过SQL自动递增,必须将force_id
参数设置为true。
下面是一个适用于某个类别的极简主义示例:
$category = new Category();
$category->id = 42;
$category->force_id = true;
$category->is_root_category = false;
$category->name = array((int)Configuration::get('PS_LANG_DEFAULT') => 'Test');
$category->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => 'test');
$category->id_parent = Configuration::get('PS_HOME_CATEGORY');
$category->add();
这将适用于所有版本的PrestaShop,并且可以在ObjectModel类(所有对象的父类)中找到:
/** @var bool Enables to define an ID before adding object. */
public $force_id = false;
然后在将对象添加到DB中的同一个类中之前对其进行检查:
public function add($auto_date = true, $null_values = false)
{
if (isset($this->id) && !$this->force_id)
unset($this->id);
...
我希望这能帮到你!
https://stackoverflow.com/questions/56329118
复制相似问题