Jump to content

Create new Category using new Category in a php


iulianv
 Share

Recommended Posts

Hello,

 

i have a question if anyone could answer it.

 

i have a piece of code that creates a new category inside a custom script..

 

like so

 

$category = new Category;
$category->id_category = 155;
$category->active = 0;
$category->id_parent = 15;
$category->name = "category";
$category->link_rewrite = "one-category";
$category->add();

 

my problem is that the id_category = 155 is not inserted into the database .. it takes the next autoincremented id instead .. like 12 .. 13.. 15 ..

 

any ideas? Thanks.

Share this post


Link to post
Share on other sites

Try this:

$category = new Category;
$category->id = 155;
$category->active = 0;
$category->id_parent = 15;
$category->name = "category";
$category->link_rewrite = "one-category";
//this will force ObjectModel to use your ID
$_GET['forceIDs'] = true;
$category->add();

Share this post


Link to post
Share on other sites

Thanks for the quick response Simone, but still no.

 

even with forceIDs=true still incremented ..

or $category = new Category(155);

or $category->id_category = 155;  :|

 

another approach?!

Share this post


Link to post
Share on other sites

  • 3 weeks later...

Here's my working code (tested on PS 1.6.0.5):

        $_GET['forceIDs'] = true;
        $cat = new Category();
        $cat->id = 100;
        $cat->description = [ 1=> 'd' ];
        $cat->id_parent = 2;
        $cat->is_root_category = false;
        $cat->link_rewrite = [ 1=> 'lr' ];
        $cat->meta_description = [ 1=> 'md' ];
        $cat->meta_keywords = [ 1=> 'mk' ];
        $cat->meta_title = [ 1=> 'mt' ];
        $cat->name = [ 1=> 'name' ];
        $save = $cat->add();
        d([$save ? $save : 'error', $cat]);
Edited by SimoneS93 (see edit history)

Share this post


Link to post
Share on other sites

Thanks for the quick response Simone, but still no.

 

even with forceIDs=true still incremented ..

or $category = new Category(155);

or $category->id_category = 155;  :|

 

another approach?!

 

 

show the code you're trying to use, everything depends on it

Share this post


Link to post
Share on other sites

  • 4 years later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More