duanmu2941 2019-05-27 15:46
浏览 108
已采纳

如何强制在类别上使用我的ID,而不是prestashop类别的自动增量。 的Prestashop

I want to define my own Category id like(999,2222,3333) and force using it on the PrestaShop database but when I execute it does(1,2,3) How can I force using my own 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();

        }
  • 写回答

1条回答 默认 最新

  • duanji8615 2019-05-27 18:10
    关注

    In PrestaShop, if you'd like to force the ID of a specific object (Category, Product, Order, etc.) to bypass the SQL auto-increment during DB insertion, you have to set the force_id parameter to true.

    Here's a minimalist example that works for a Category:

    $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();
    

    This will work with all versions of PrestaShop and can be found in the ObjectModel class (parent class for all Objects):

    /** @var bool Enables to define an ID before adding object. */
    public $force_id = false;
    

    It is then checked right before adding the object in DB, in the same class:

    public function add($auto_date = true, $null_values = false)
    {
        if (isset($this->id) && !$this->force_id)
            unset($this->id);
    
        ...
    

    I hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?