-1

I am trying to create a node programatically like so,

$newNode = (object) NULL;
$newNode->type = 'job';
$newNode->title = $data['JobTitle'];
$newNode->uid = $user->uid;
$newNode->created = strtotime("now");
$newNode->changed = strtotime("now");
$newNode->status = 1;
$newNode->comment = 0;
$newNode->promote = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;
$newNode->tid = 0;

$newNode->summary['und'][0]['value'] = $data['JobSummary'];
$newNode->body['und'][0]['value'] = $data['JobDescription'];
$newNode->field_employment_type['und'] = strtolower($data['JobType']);
$newNode->field_job_reference['und'][0]['value'] = $data['JobReference'];
$newNode->field_salary['und'][0]['value'] = "";
$newNode->field_salary_from['und'][0]['value'] = $data['SalaryFrom'];
$newNode->field_salary_to['und'][0]['value'] = $data['SalaryTo'];
$newNode->field_salary_override['und'][0]['value'] = $data['Salary'];
$newNode->field_application_email['und'][0]['value'] = $data['ApplicationEmail'];
$newNode->field_job_category['und'][2] = 2;
$newNode->field_job_category['und'][4] = 4;

//die(print_r($newNode));
// save node
node_save($newNode);

Here I have potentially 4 taxonomies to pick from (their id indicated in brackets) Creative (2), Technical (3), Marketing (4), Client Services (6).

On node_save I am getting the following error,

500 Internal Server Error : An error occurred (23000): SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tid' cannot be null

How do I overcome this this I would have thought setting the field_job_category to the id of the taxonomy would be enough?

Udders
  • 6,914
  • 24
  • 102
  • 194

2 Answers2

0

Delete $newNode->tid = 0; and prepare object :

$newNode = new stdClass();
$newNode->type = 'job';

node_object_prepare($node);

$newNode->title = $data['JobTitle'];
$newNode->uid = $user->uid;
$newNode->created = strtotime("now");
$newNode->changed = strtotime("now");
$newNode->status = 1;
$newNode->comment = 0;
$newNode->promote = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;

$newNode->summary['und'][0]['value'] = $data['JobSummary'];
$newNode->body['und'][0]['value'] = $data['JobDescription'];
$newNode->field_employment_type['und'] = strtolower($data['JobType']);
$newNode->field_job_reference['und'][0]['value'] = $data['JobReference'];
$newNode->field_salary['und'][0]['value'] = "";
$newNode->field_salary_from['und'][0]['value'] = $data['SalaryFrom'];
$newNode->field_salary_to['und'][0]['value'] = $data['SalaryTo'];
$newNode->field_salary_override['und'][0]['value'] = $data['Salary'];
$newNode->field_application_email['und'][0]['value'] = $data['ApplicationEmail'];
$newNode->field_job_category['und'][]['tid'] = 2; // right syntax
$newNode->field_job_category['und'][]['tid']= 4;  // right syntax

//die(print_r($newNode));
// save node
node_save($newNode);
Fky
  • 2,133
  • 1
  • 15
  • 23
  • while this get rid of the error, it doesnt actually select a taxonomy correctly, when I go to edit the node within the admin area, none of the job category taxonomies are selected. – Udders Sep 21 '18 at 17:56
0

If you want to have only one term id associated:

$newNode->field_category[LANGUAGE_NONE][0]['tid'] = <actual term id>

If you have multiple term ids to associate: Iterate over your array of term ids, and increment index. Something like:

$newNode->field_category[LANGUAGE_NONE][0]['tid'] = <actual term id>
$newNode->field_category[LANGUAGE_NONE][1]['tid'] = <actual term id>
$newNode->field_category[LANGUAGE_NONE][2]['tid'] = <actual term id>

Note the index values above.

And, I usually set following attributes set for a new node:

Gorav Singal
  • 508
  • 3
  • 11