Forum

Thread tagged as: Error, Api

Creating categories in code

Hi,

I'm setting up a site which will populate categories and collections from a third party, via it's API. In code, I want to create new categories. To do so, it looks like I should be able to use PerchCategories_Categories->find_or_create. However, in the current version of Runway (3.0.9) it doesn't seem right - though of course I might just be misusing the method :)

I'm calling it like:

$Categories = new PerchCategories_Categories($API); $NewCategory = find_or_create('property-location/matts-newtown', 'matts-newtown', $Categories );

There seems to be two problems: First, I think there's an erroneous array_pop on line 41, which causes one of the values sent to create to be empty; And secondly, the data passed in to the create function at the end of find_or_create is lacking a catDynamicFields value, which causes the error "Invalid query: SQLSTATE[HY000]: General error: 1364 Field 'catDynamicFields' doesn't have a default value"

Hope that's of interest, or someone can correct my use of the method?

Matt Keehan

Matt Keehan 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That's not part of the public API, so it's not surprising you're having issues. I'm presuming your example isn't actually how you're calling it, because you're calling a global find_or_create() function and not the method on PerchCategories_Categories.

I think the array_pop is only erroneous for you because your category path is erroneous. You're missing the trailing slash it looks like to me. You're also passing too many arguments.

The "default value" issue could be an unrelated issue due to your version of MySQL. Can you create categories via the UI?

Thanks Drew,

Oops, you're right, that was my own method. I'm calling it like this

$NewCategory = $Categories->find_or_create('property-location/mattss-newtown', 'matts new town');

I added the slash and it looks happy, though I'm still seeing the mysql error - I'll check my version vs required.

Cheers, Matt.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I suspect it's newer, forcing more constraints than the code is expecting. Can you create categories via the UI?

Oops yeah didn't mention, yes I can create categories via the UI. I don't think this function is used for that though?

Drew McLellan

Drew McLellan 2638 points
Perch Support

No, it's only used for WordPress import from Blog. As I say, it's not part of the public API.

Cool ok. I'm using MySql 5.6 on a Mac, tables are set to MyISAM & utf8. I think that should be ok? But I'll stop messing with it anyway - thanks again :)

I've been looking some more - sorry - the install script (perch/setup/sql/perch.sql) sets the field as 'catDynamicFields' text NOT NULL,, but the data passed to create in find_or_create only has these fields

$data = array( 'setID' => $Set->id(), 'catTitle' => $label, 'catSlug' => $catSlug, 'catPath' => $catPath, 'catDisplayPath' => '', 'catParentID' => 0, );

so it doesn't look like it could work. I've added 'catDynamicFields' => '{"desc":{"_flang":"markdown","raw":"","processed":""}}' to that array (I copied the other entries), and that works, though I know as it's in core it would be lost in an update. I could set mysql to set the field to an empty string or that string by default - do you think that would be better?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, we can't offer any support if you hack at the codebase.